1
This is v4 of my notdirty + rom patch set with two suggested name
1
The following changes since commit e93ded1bf6c94ab95015b33e188bc8b0b0c32670:
2
changes (qemu_build_not_reached, TLB_DISCARD_WRITE) from David and Alex.
3
2
4
3
Merge tag 'testing-pull-request-2022-08-30' of https://gitlab.com/thuth/qemu into staging (2022-08-31 18:19:03 -0400)
5
r~
6
7
8
The following changes since commit 240ab11fb72049d6373cbbec8d788f8e411a00bc:
9
10
Merge remote-tracking branch 'remotes/aperard/tags/pull-xen-20190924' into staging (2019-09-24 15:36:31 +0100)
11
4
12
are available in the Git repository at:
5
are available in the Git repository at:
13
6
14
https://github.com/rth7680/qemu.git tags/pull-tcg-20190925
7
https://gitlab.com/rth7680/qemu.git tags/pull-tcg-20220901
15
8
16
for you to fetch changes up to ae57db63acf5a0399232f852acc5c1d83ef63400:
9
for you to fetch changes up to 20011be2e30b8aa8ef1fc258485f00c688703deb:
17
10
18
cputlb: Pass retaddr to tb_check_watchpoint (2019-09-25 10:56:28 -0700)
11
target/riscv: Make translator stop before the end of a page (2022-09-01 07:43:08 +0100)
19
12
20
----------------------------------------------------------------
13
----------------------------------------------------------------
21
Fixes for TLB_BSWAP
14
Respect PROT_EXEC in user-only mode.
22
Coversion of NOTDIRTY and ROM handling to cputlb
15
Fix s390x, i386 and riscv for translations crossing a page.
23
Followup cleanups to cputlb
24
16
25
----------------------------------------------------------------
17
----------------------------------------------------------------
18
Ilya Leoshkevich (4):
19
linux-user: Clear translations on mprotect()
20
accel/tcg: Introduce is_same_page()
21
target/s390x: Make translator stop before the end of a page
22
target/i386: Make translator stop before the end of a page
23
26
Richard Henderson (16):
24
Richard Henderson (16):
27
exec: Use TARGET_PAGE_BITS_MIN for TLB flags
25
linux-user/arm: Mark the commpage executable
28
cputlb: Disable __always_inline__ without optimization
26
linux-user/hppa: Allocate page zero as a commpage
29
qemu/compiler.h: Add qemu_build_not_reached
27
linux-user/x86_64: Allocate vsyscall page as a commpage
30
cputlb: Use qemu_build_not_reached in load/store_helpers
28
linux-user: Honor PT_GNU_STACK
31
cputlb: Split out load/store_memop
29
tests/tcg/i386: Move smc_code2 to an executable section
32
cputlb: Introduce TLB_BSWAP
30
accel/tcg: Properly implement get_page_addr_code for user-only
33
exec: Adjust notdirty tracing
31
accel/tcg: Unlock mmap_lock after longjmp
34
cputlb: Move ROM handling from I/O path to TLB path
32
accel/tcg: Make tb_htable_lookup static
35
cputlb: Move NOTDIRTY handling from I/O path to TLB path
33
accel/tcg: Move qemu_ram_addr_from_host_nofail to physmem.c
36
cputlb: Partially inline memory_region_section_get_iotlb
34
accel/tcg: Use probe_access_internal for softmmu get_page_addr_code_hostp
37
cputlb: Merge and move memory_notdirty_write_{prepare,complete}
35
accel/tcg: Document the faulting lookup in tb_lookup_cmp
38
cputlb: Handle TLB_NOTDIRTY in probe_access
36
accel/tcg: Remove translator_ldsw
39
cputlb: Remove cpu->mem_io_vaddr
37
accel/tcg: Add pc and host_pc params to gen_intermediate_code
40
cputlb: Remove tb_invalidate_phys_page_range is_cpu_write_access
38
accel/tcg: Add fast path for translator_ld*
41
cputlb: Pass retaddr to tb_invalidate_phys_page_fast
39
target/riscv: Add MAX_INSN_LEN and insn_len
42
cputlb: Pass retaddr to tb_check_watchpoint
40
target/riscv: Make translator stop before the end of a page
43
41
44
accel/tcg/translate-all.h | 8 +-
42
include/elf.h | 1 +
45
include/exec/cpu-all.h | 23 ++-
43
include/exec/cpu-common.h | 1 +
46
include/exec/cpu-common.h | 3 -
44
include/exec/exec-all.h | 89 ++++++++----------------
47
include/exec/exec-all.h | 6 +-
45
include/exec/translator.h | 96 ++++++++++++++++---------
48
include/exec/memory-internal.h | 65 --------
46
linux-user/arm/target_cpu.h | 4 +-
49
include/hw/core/cpu.h | 2 -
47
linux-user/qemu.h | 1 +
50
include/qemu/compiler.h | 26 +++
48
accel/tcg/cpu-exec.c | 143 ++++++++++++++++++++------------------
51
accel/tcg/cputlb.c | 348 +++++++++++++++++++++++++----------------
49
accel/tcg/cputlb.c | 93 +++++++------------------
52
accel/tcg/translate-all.c | 51 +++---
50
accel/tcg/translate-all.c | 29 ++++----
53
exec.c | 158 +------------------
51
accel/tcg/translator.c | 135 ++++++++++++++++++++++++++---------
54
hw/core/cpu.c | 1 -
52
accel/tcg/user-exec.c | 17 ++++-
55
memory.c | 20 ---
53
linux-user/elfload.c | 82 ++++++++++++++++++++--
56
trace-events | 4 +-
54
linux-user/mmap.c | 6 +-
57
13 files changed, 288 insertions(+), 427 deletions(-)
55
softmmu/physmem.c | 12 ++++
58
56
target/alpha/translate.c | 5 +-
57
target/arm/translate.c | 5 +-
58
target/avr/translate.c | 5 +-
59
target/cris/translate.c | 5 +-
60
target/hexagon/translate.c | 6 +-
61
target/hppa/translate.c | 5 +-
62
target/i386/tcg/translate.c | 71 +++++++++++--------
63
target/loongarch/translate.c | 6 +-
64
target/m68k/translate.c | 5 +-
65
target/microblaze/translate.c | 5 +-
66
target/mips/tcg/translate.c | 5 +-
67
target/nios2/translate.c | 5 +-
68
target/openrisc/translate.c | 6 +-
69
target/ppc/translate.c | 5 +-
70
target/riscv/translate.c | 32 +++++++--
71
target/rx/translate.c | 5 +-
72
target/s390x/tcg/translate.c | 20 ++++--
73
target/sh4/translate.c | 5 +-
74
target/sparc/translate.c | 5 +-
75
target/tricore/translate.c | 6 +-
76
target/xtensa/translate.c | 6 +-
77
tests/tcg/i386/test-i386.c | 2 +-
78
tests/tcg/riscv64/noexec.c | 79 +++++++++++++++++++++
79
tests/tcg/s390x/noexec.c | 106 ++++++++++++++++++++++++++++
80
tests/tcg/x86_64/noexec.c | 75 ++++++++++++++++++++
81
tests/tcg/multiarch/noexec.c.inc | 139 ++++++++++++++++++++++++++++++++++++
82
tests/tcg/riscv64/Makefile.target | 1 +
83
tests/tcg/s390x/Makefile.target | 1 +
84
tests/tcg/x86_64/Makefile.target | 3 +-
85
43 files changed, 966 insertions(+), 367 deletions(-)
86
create mode 100644 tests/tcg/riscv64/noexec.c
87
create mode 100644 tests/tcg/s390x/noexec.c
88
create mode 100644 tests/tcg/x86_64/noexec.c
89
create mode 100644 tests/tcg/multiarch/noexec.c.inc
diff view generated by jsdifflib
1
Rather than rely on cpu->mem_io_pc, pass retaddr down directly.
1
We're about to start validating PAGE_EXEC, which means
2
that we've got to mark the commpage executable. We had
3
been placing the commpage outside of reserved_va, which
4
was incorrect and lead to an abort.
2
5
3
Within tb_invalidate_phys_page_range__locked, the is_cpu_write_access
6
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
4
parameter is non-zero exactly when retaddr would be non-zero, so that
7
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
5
is a simple replacement.
6
7
Recognize that current_tb_not_found is true only when mem_io_pc
8
(and now retaddr) are also non-zero, so remove a redundant test.
9
10
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
11
Reviewed-by: David Hildenbrand <david@redhat.com>
12
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
8
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
13
---
9
---
14
accel/tcg/translate-all.h | 3 ++-
10
linux-user/arm/target_cpu.h | 4 ++--
15
accel/tcg/cputlb.c | 6 +-----
11
linux-user/elfload.c | 6 +++++-
16
accel/tcg/translate-all.c | 39 +++++++++++++++++++--------------------
12
2 files changed, 7 insertions(+), 3 deletions(-)
17
3 files changed, 22 insertions(+), 26 deletions(-)
18
13
19
diff --git a/accel/tcg/translate-all.h b/accel/tcg/translate-all.h
14
diff --git a/linux-user/arm/target_cpu.h b/linux-user/arm/target_cpu.h
20
index XXXXXXX..XXXXXXX 100644
15
index XXXXXXX..XXXXXXX 100644
21
--- a/accel/tcg/translate-all.h
16
--- a/linux-user/arm/target_cpu.h
22
+++ b/accel/tcg/translate-all.h
17
+++ b/linux-user/arm/target_cpu.h
23
@@ -XXX,XX +XXX,XX @@ struct page_collection *page_collection_lock(tb_page_addr_t start,
18
@@ -XXX,XX +XXX,XX @@ static inline unsigned long arm_max_reserved_va(CPUState *cs)
24
tb_page_addr_t end);
25
void page_collection_unlock(struct page_collection *set);
26
void tb_invalidate_phys_page_fast(struct page_collection *pages,
27
- tb_page_addr_t start, int len);
28
+ tb_page_addr_t start, int len,
29
+ uintptr_t retaddr);
30
void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end);
31
void tb_check_watchpoint(CPUState *cpu);
32
33
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
34
index XXXXXXX..XXXXXXX 100644
35
--- a/accel/tcg/cputlb.c
36
+++ b/accel/tcg/cputlb.c
37
@@ -XXX,XX +XXX,XX @@ static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size,
38
if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) {
39
struct page_collection *pages
40
= page_collection_lock(ram_addr, ram_addr + size);
41
-
42
- /* We require mem_io_pc in tb_invalidate_phys_page_range. */
43
- cpu->mem_io_pc = retaddr;
44
-
45
- tb_invalidate_phys_page_fast(pages, ram_addr, size);
46
+ tb_invalidate_phys_page_fast(pages, ram_addr, size, retaddr);
47
page_collection_unlock(pages);
48
}
49
50
diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c
51
index XXXXXXX..XXXXXXX 100644
52
--- a/accel/tcg/translate-all.c
53
+++ b/accel/tcg/translate-all.c
54
@@ -XXX,XX +XXX,XX @@ static void
55
tb_invalidate_phys_page_range__locked(struct page_collection *pages,
56
PageDesc *p, tb_page_addr_t start,
57
tb_page_addr_t end,
58
- int is_cpu_write_access)
59
+ uintptr_t retaddr)
60
{
61
TranslationBlock *tb;
62
tb_page_addr_t tb_start, tb_end;
63
@@ -XXX,XX +XXX,XX @@ tb_invalidate_phys_page_range__locked(struct page_collection *pages,
64
#ifdef TARGET_HAS_PRECISE_SMC
65
CPUState *cpu = current_cpu;
66
CPUArchState *env = NULL;
67
- int current_tb_not_found = is_cpu_write_access;
68
+ bool current_tb_not_found = retaddr != 0;
69
+ bool current_tb_modified = false;
70
TranslationBlock *current_tb = NULL;
71
- int current_tb_modified = 0;
72
target_ulong current_pc = 0;
73
target_ulong current_cs_base = 0;
74
uint32_t current_flags = 0;
75
@@ -XXX,XX +XXX,XX @@ tb_invalidate_phys_page_range__locked(struct page_collection *pages,
76
if (!(tb_end <= start || tb_start >= end)) {
77
#ifdef TARGET_HAS_PRECISE_SMC
78
if (current_tb_not_found) {
79
- current_tb_not_found = 0;
80
- current_tb = NULL;
81
- if (cpu->mem_io_pc) {
82
- /* now we have a real cpu fault */
83
- current_tb = tcg_tb_lookup(cpu->mem_io_pc);
84
- }
85
+ current_tb_not_found = false;
86
+ /* now we have a real cpu fault */
87
+ current_tb = tcg_tb_lookup(retaddr);
88
}
89
if (current_tb == tb &&
90
(tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
91
- /* If we are modifying the current TB, we must stop
92
- its execution. We could be more precise by checking
93
- that the modification is after the current PC, but it
94
- would require a specialized function to partially
95
- restore the CPU state */
96
-
97
- current_tb_modified = 1;
98
- cpu_restore_state_from_tb(cpu, current_tb,
99
- cpu->mem_io_pc, true);
100
+ /*
101
+ * If we are modifying the current TB, we must stop
102
+ * its execution. We could be more precise by checking
103
+ * that the modification is after the current PC, but it
104
+ * would require a specialized function to partially
105
+ * restore the CPU state.
106
+ */
107
+ current_tb_modified = true;
108
+ cpu_restore_state_from_tb(cpu, current_tb, retaddr, true);
109
cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
110
&current_flags);
111
}
112
@@ -XXX,XX +XXX,XX @@ void tb_invalidate_phys_range(target_ulong start, target_ulong end)
113
* Call with all @pages in the range [@start, @start + len[ locked.
114
*/
115
void tb_invalidate_phys_page_fast(struct page_collection *pages,
116
- tb_page_addr_t start, int len)
117
+ tb_page_addr_t start, int len,
118
+ uintptr_t retaddr)
119
{
120
PageDesc *p;
121
122
@@ -XXX,XX +XXX,XX @@ void tb_invalidate_phys_page_fast(struct page_collection *pages,
123
}
124
} else {
19
} else {
125
do_invalidate:
20
/*
126
- tb_invalidate_phys_page_range__locked(pages, p, start, start + len, 1);
21
* We need to be able to map the commpage.
127
+ tb_invalidate_phys_page_range__locked(pages, p, start, start + len,
22
- * See validate_guest_space in linux-user/elfload.c.
128
+ retaddr);
23
+ * See init_guest_commpage in linux-user/elfload.c.
24
*/
25
- return 0xffff0000ul;
26
+ return 0xfffffffful;
129
}
27
}
130
}
28
}
131
#else
29
#define MAX_RESERVED_VA arm_max_reserved_va
30
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
31
index XXXXXXX..XXXXXXX 100644
32
--- a/linux-user/elfload.c
33
+++ b/linux-user/elfload.c
34
@@ -XXX,XX +XXX,XX @@ enum {
35
36
static bool init_guest_commpage(void)
37
{
38
- void *want = g2h_untagged(HI_COMMPAGE & -qemu_host_page_size);
39
+ abi_ptr commpage = HI_COMMPAGE & -qemu_host_page_size;
40
+ void *want = g2h_untagged(commpage);
41
void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
42
MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
43
44
@@ -XXX,XX +XXX,XX @@ static bool init_guest_commpage(void)
45
perror("Protecting guest commpage");
46
exit(EXIT_FAILURE);
47
}
48
+
49
+ page_set_flags(commpage, commpage + qemu_host_page_size,
50
+ PAGE_READ | PAGE_EXEC | PAGE_VALID);
51
return true;
52
}
53
132
--
54
--
133
2.17.1
55
2.34.1
134
135
diff view generated by jsdifflib
1
Increase the current runtime assert to a compile-time assert.
1
While there are no target-specific nonfaulting probes,
2
generic code may grow some uses at some point.
2
3
3
Reviewed-by: David Hildenbrand <david@redhat.com>
4
Note that the attrs argument was incorrect -- it should have
4
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
5
been MEMTXATTRS_UNSPECIFIED. Just use the simpler interface.
6
7
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
5
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
8
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
6
---
9
---
7
accel/tcg/cputlb.c | 5 ++---
10
target/avr/helper.c | 46 ++++++++++++++++++++++++++++-----------------
8
1 file changed, 2 insertions(+), 3 deletions(-)
11
1 file changed, 29 insertions(+), 17 deletions(-)
9
12
10
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
13
diff --git a/target/avr/helper.c b/target/avr/helper.c
11
index XXXXXXX..XXXXXXX 100644
14
index XXXXXXX..XXXXXXX 100644
12
--- a/accel/tcg/cputlb.c
15
--- a/target/avr/helper.c
13
+++ b/accel/tcg/cputlb.c
16
+++ b/target/avr/helper.c
14
@@ -XXX,XX +XXX,XX @@ load_helper(CPUArchState *env, target_ulong addr, TCGMemOpIdx oi,
17
@@ -XXX,XX +XXX,XX @@ bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
15
res = ldq_le_p(haddr);
18
MMUAccessType access_type, int mmu_idx,
16
break;
19
bool probe, uintptr_t retaddr)
17
default:
20
{
18
- g_assert_not_reached();
21
- int prot = 0;
19
+ qemu_build_not_reached();
22
- MemTxAttrs attrs = {};
23
+ int prot, page_size = TARGET_PAGE_SIZE;
24
uint32_t paddr;
25
26
address &= TARGET_PAGE_MASK;
27
28
if (mmu_idx == MMU_CODE_IDX) {
29
- /* access to code in flash */
30
+ /* Access to code in flash. */
31
paddr = OFFSET_CODE + address;
32
prot = PAGE_READ | PAGE_EXEC;
33
- if (paddr + TARGET_PAGE_SIZE > OFFSET_DATA) {
34
+ if (paddr >= OFFSET_DATA) {
35
+ /*
36
+ * This should not be possible via any architectural operations.
37
+ * There is certainly not an exception that we can deliver.
38
+ * Accept probing that might come from generic code.
39
+ */
40
+ if (probe) {
41
+ return false;
42
+ }
43
error_report("execution left flash memory");
44
abort();
45
}
46
- } else if (address < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
47
- /*
48
- * access to CPU registers, exit and rebuilt this TB to use full access
49
- * incase it touches specially handled registers like SREG or SP
50
- */
51
- AVRCPU *cpu = AVR_CPU(cs);
52
- CPUAVRState *env = &cpu->env;
53
- env->fullacc = 1;
54
- cpu_loop_exit_restore(cs, retaddr);
55
} else {
56
- /* access to memory. nothing special */
57
+ /* Access to memory. */
58
paddr = OFFSET_DATA + address;
59
prot = PAGE_READ | PAGE_WRITE;
60
+ if (address < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
61
+ /*
62
+ * Access to CPU registers, exit and rebuilt this TB to use
63
+ * full access in case it touches specially handled registers
64
+ * like SREG or SP. For probing, set page_size = 1, in order
65
+ * to force tlb_fill to be called for the next access.
66
+ */
67
+ if (probe) {
68
+ page_size = 1;
69
+ } else {
70
+ AVRCPU *cpu = AVR_CPU(cs);
71
+ CPUAVRState *env = &cpu->env;
72
+ env->fullacc = 1;
73
+ cpu_loop_exit_restore(cs, retaddr);
74
+ }
75
+ }
20
}
76
}
21
77
22
return res;
78
- tlb_set_page_with_attrs(cs, address, paddr, attrs, prot,
23
@@ -XXX,XX +XXX,XX @@ store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
79
- mmu_idx, TARGET_PAGE_SIZE);
24
stq_le_p(haddr, val);
80
-
25
break;
81
+ tlb_set_page(cs, address, paddr, prot, mmu_idx, page_size);
26
default:
82
return true;
27
- g_assert_not_reached();
28
- break;
29
+ qemu_build_not_reached();
30
}
31
}
83
}
32
84
33
--
85
--
34
2.17.1
86
2.34.1
35
87
36
88
diff view generated by jsdifflib
1
These bits do not need to vary with the actual page size
1
There is no need to go through cc->tcg_ops when
2
used by the guest.
2
we know what value that must have.
3
3
4
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
4
Reviewed-by: Michael Rolnik <mrolnik@gmail.com>
5
Reviewed-by: David Hildenbrand <david@redhat.com>
5
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
6
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
7
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
6
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
8
---
7
---
9
include/exec/cpu-all.h | 16 ++++++++++------
8
target/avr/helper.c | 5 ++---
10
1 file changed, 10 insertions(+), 6 deletions(-)
9
1 file changed, 2 insertions(+), 3 deletions(-)
11
10
12
diff --git a/include/exec/cpu-all.h b/include/exec/cpu-all.h
11
diff --git a/target/avr/helper.c b/target/avr/helper.c
13
index XXXXXXX..XXXXXXX 100644
12
index XXXXXXX..XXXXXXX 100644
14
--- a/include/exec/cpu-all.h
13
--- a/target/avr/helper.c
15
+++ b/include/exec/cpu-all.h
14
+++ b/target/avr/helper.c
16
@@ -XXX,XX +XXX,XX @@ CPUArchState *cpu_copy(CPUArchState *env);
15
@@ -XXX,XX +XXX,XX @@
17
16
bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
18
#if !defined(CONFIG_USER_ONLY)
17
{
19
18
bool ret = false;
20
-/* Flags stored in the low bits of the TLB virtual address. These are
19
- CPUClass *cc = CPU_GET_CLASS(cs);
21
- * defined so that fast path ram access is all zeros.
20
AVRCPU *cpu = AVR_CPU(cs);
22
+/*
21
CPUAVRState *env = &cpu->env;
23
+ * Flags stored in the low bits of the TLB virtual address.
22
24
+ * These are defined so that fast path ram access is all zeros.
23
if (interrupt_request & CPU_INTERRUPT_RESET) {
25
* The flags all must be between TARGET_PAGE_BITS and
24
if (cpu_interrupts_enabled(env)) {
26
* maximum address alignment bit.
25
cs->exception_index = EXCP_RESET;
27
+ *
26
- cc->tcg_ops->do_interrupt(cs);
28
+ * Use TARGET_PAGE_BITS_MIN so that these bits are constant
27
+ avr_cpu_do_interrupt(cs);
29
+ * when TARGET_PAGE_BITS_VARY is in effect.
28
30
*/
29
cs->interrupt_request &= ~CPU_INTERRUPT_RESET;
31
/* Zero if TLB entry is valid. */
30
32
-#define TLB_INVALID_MASK (1 << (TARGET_PAGE_BITS - 1))
31
@@ -XXX,XX +XXX,XX @@ bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
33
+#define TLB_INVALID_MASK (1 << (TARGET_PAGE_BITS_MIN - 1))
32
if (cpu_interrupts_enabled(env) && env->intsrc != 0) {
34
/* Set if TLB entry references a clean RAM page. The iotlb entry will
33
int index = ctz32(env->intsrc);
35
contain the page physical address. */
34
cs->exception_index = EXCP_INT(index);
36
-#define TLB_NOTDIRTY (1 << (TARGET_PAGE_BITS - 2))
35
- cc->tcg_ops->do_interrupt(cs);
37
+#define TLB_NOTDIRTY (1 << (TARGET_PAGE_BITS_MIN - 2))
36
+ avr_cpu_do_interrupt(cs);
38
/* Set if TLB entry is an IO callback. */
37
39
-#define TLB_MMIO (1 << (TARGET_PAGE_BITS - 3))
38
env->intsrc &= env->intsrc - 1; /* clear the interrupt */
40
+#define TLB_MMIO (1 << (TARGET_PAGE_BITS_MIN - 3))
39
if (!env->intsrc) {
41
/* Set if TLB entry contains a watchpoint. */
42
-#define TLB_WATCHPOINT (1 << (TARGET_PAGE_BITS - 4))
43
+#define TLB_WATCHPOINT (1 << (TARGET_PAGE_BITS_MIN - 4))
44
45
/* Use this mask to check interception with an alignment mask
46
* in a TCG backend.
47
--
40
--
48
2.17.1
41
2.34.1
49
42
50
43
diff view generated by jsdifflib
1
Since 9458a9a1df1a, all readers of the dirty bitmaps wait
1
We're about to start validating PAGE_EXEC, which means that we've
2
for the rcu lock, which means that they wait until the end
2
got to mark page zero executable. We had been special casing this
3
of any executing TranslationBlock.
3
entirely within translate.
4
4
5
As a consequence, there is no need for the actual access
5
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
6
to happen in between the _prepare and _complete. Therefore,
6
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
7
we can improve things by merging the two functions into
8
notdirty_write and dropping the NotDirtyInfo structure.
9
10
In addition, the only users of notdirty_write are in cputlb.c,
11
so move the merged function there. Pass in the CPUIOTLBEntry
12
from which the ram_addr_t may be computed.
13
14
Reviewed-by: David Hildenbrand <david@redhat.com>
15
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
16
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
7
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
17
---
8
---
18
include/exec/memory-internal.h | 65 -----------------------------
9
linux-user/elfload.c | 34 +++++++++++++++++++++++++++++++---
19
accel/tcg/cputlb.c | 76 +++++++++++++++++++---------------
10
1 file changed, 31 insertions(+), 3 deletions(-)
20
exec.c | 44 --------------------
21
3 files changed, 42 insertions(+), 143 deletions(-)
22
11
23
diff --git a/include/exec/memory-internal.h b/include/exec/memory-internal.h
12
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
24
index XXXXXXX..XXXXXXX 100644
13
index XXXXXXX..XXXXXXX 100644
25
--- a/include/exec/memory-internal.h
14
--- a/linux-user/elfload.c
26
+++ b/include/exec/memory-internal.h
15
+++ b/linux-user/elfload.c
27
@@ -XXX,XX +XXX,XX @@ void address_space_dispatch_free(AddressSpaceDispatch *d);
16
@@ -XXX,XX +XXX,XX @@ static inline void init_thread(struct target_pt_regs *regs,
28
17
regs->gr[31] = infop->entry;
29
void mtree_print_dispatch(struct AddressSpaceDispatch *d,
30
MemoryRegion *root);
31
-
32
-struct page_collection;
33
-
34
-/* Opaque struct for passing info from memory_notdirty_write_prepare()
35
- * to memory_notdirty_write_complete(). Callers should treat all fields
36
- * as private, with the exception of @active.
37
- *
38
- * @active is a field which is not touched by either the prepare or
39
- * complete functions, but which the caller can use if it wishes to
40
- * track whether it has called prepare for this struct and so needs
41
- * to later call the complete function.
42
- */
43
-typedef struct {
44
- CPUState *cpu;
45
- struct page_collection *pages;
46
- ram_addr_t ram_addr;
47
- vaddr mem_vaddr;
48
- unsigned size;
49
- bool active;
50
-} NotDirtyInfo;
51
-
52
-/**
53
- * memory_notdirty_write_prepare: call before writing to non-dirty memory
54
- * @ndi: pointer to opaque NotDirtyInfo struct
55
- * @cpu: CPU doing the write
56
- * @mem_vaddr: virtual address of write
57
- * @ram_addr: the ram address of the write
58
- * @size: size of write in bytes
59
- *
60
- * Any code which writes to the host memory corresponding to
61
- * guest RAM which has been marked as NOTDIRTY must wrap those
62
- * writes in calls to memory_notdirty_write_prepare() and
63
- * memory_notdirty_write_complete():
64
- *
65
- * NotDirtyInfo ndi;
66
- * memory_notdirty_write_prepare(&ndi, ....);
67
- * ... perform write here ...
68
- * memory_notdirty_write_complete(&ndi);
69
- *
70
- * These calls will ensure that we flush any TCG translated code for
71
- * the memory being written, update the dirty bits and (if possible)
72
- * remove the slowpath callback for writing to the memory.
73
- *
74
- * This must only be called if we are using TCG; it will assert otherwise.
75
- *
76
- * We may take locks in the prepare call, so callers must ensure that
77
- * they don't exit (via longjump or otherwise) without calling complete.
78
- *
79
- * This call must only be made inside an RCU critical section.
80
- * (Note that while we're executing a TCG TB we're always in an
81
- * RCU critical section, which is likely to be the case for callers
82
- * of these functions.)
83
- */
84
-void memory_notdirty_write_prepare(NotDirtyInfo *ndi,
85
- CPUState *cpu,
86
- vaddr mem_vaddr,
87
- ram_addr_t ram_addr,
88
- unsigned size);
89
-/**
90
- * memory_notdirty_write_complete: finish write to non-dirty memory
91
- * @ndi: pointer to the opaque NotDirtyInfo struct which was initialized
92
- * by memory_not_dirty_write_prepare().
93
- */
94
-void memory_notdirty_write_complete(NotDirtyInfo *ndi);
95
-
96
#endif
97
#endif
98
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
99
index XXXXXXX..XXXXXXX 100644
100
--- a/accel/tcg/cputlb.c
101
+++ b/accel/tcg/cputlb.c
102
@@ -XXX,XX +XXX,XX @@
103
#include "exec/helper-proto.h"
104
#include "qemu/atomic.h"
105
#include "qemu/atomic128.h"
106
+#include "translate-all.h"
107
108
/* DEBUG defines, enable DEBUG_TLB_LOG to log to the CPU_LOG_MMU target */
109
/* #define DEBUG_TLB */
110
@@ -XXX,XX +XXX,XX @@ tb_page_addr_t get_page_addr_code(CPUArchState *env, target_ulong addr)
111
return qemu_ram_addr_from_host_nofail(p);
112
}
18
}
113
19
114
+static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size,
20
+#define LO_COMMPAGE 0
115
+ CPUIOTLBEntry *iotlbentry, uintptr_t retaddr)
21
+
22
+static bool init_guest_commpage(void)
116
+{
23
+{
117
+ ram_addr_t ram_addr = mem_vaddr + iotlbentry->addr;
24
+ void *want = g2h_untagged(LO_COMMPAGE);
25
+ void *addr = mmap(want, qemu_host_page_size, PROT_NONE,
26
+ MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
118
+
27
+
119
+ trace_memory_notdirty_write_access(mem_vaddr, ram_addr, size);
28
+ if (addr == MAP_FAILED) {
120
+
29
+ perror("Allocating guest commpage");
121
+ if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) {
30
+ exit(EXIT_FAILURE);
122
+ struct page_collection *pages
31
+ }
123
+ = page_collection_lock(ram_addr, ram_addr + size);
32
+ if (addr != want) {
124
+
33
+ return false;
125
+ /* We require mem_io_pc in tb_invalidate_phys_page_range. */
126
+ cpu->mem_io_pc = retaddr;
127
+
128
+ tb_invalidate_phys_page_fast(pages, ram_addr, size);
129
+ page_collection_unlock(pages);
130
+ }
34
+ }
131
+
35
+
132
+ /*
36
+ /*
133
+ * Set both VGA and migration bits for simplicity and to remove
37
+ * On Linux, page zero is normally marked execute only + gateway.
134
+ * the notdirty callback faster.
38
+ * Normal read or write is supposed to fail (thus PROT_NONE above),
39
+ * but specific offsets have kernel code mapped to raise permissions
40
+ * and implement syscalls. Here, simply mark the page executable.
41
+ * Special case the entry points during translation (see do_page_zero).
135
+ */
42
+ */
136
+ cpu_physical_memory_set_dirty_range(ram_addr, size, DIRTY_CLIENTS_NOCODE);
43
+ page_set_flags(LO_COMMPAGE, LO_COMMPAGE + TARGET_PAGE_SIZE,
137
+
44
+ PAGE_EXEC | PAGE_VALID);
138
+ /* We remove the notdirty callback only if the code has been flushed. */
45
+ return true;
139
+ if (!cpu_physical_memory_is_clean(ram_addr)) {
140
+ trace_memory_notdirty_set_dirty(mem_vaddr);
141
+ tlb_set_dirty(cpu, mem_vaddr);
142
+ }
143
+}
46
+}
144
+
47
+
145
/*
48
#endif /* TARGET_HPPA */
146
* Probe for whether the specified guest access is permitted. If it is not
49
147
* permitted then an exception will be taken in the same way as if this
50
#ifdef TARGET_XTENSA
148
@@ -XXX,XX +XXX,XX @@ void *tlb_vaddr_to_host(CPUArchState *env, abi_ptr addr,
51
@@ -XXX,XX +XXX,XX @@ static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
149
/* Probe for a read-modify-write atomic operation. Do not allow unaligned
52
}
150
* operations, or io operations to proceed. Return the host address. */
53
151
static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr,
54
#if defined(HI_COMMPAGE)
152
- TCGMemOpIdx oi, uintptr_t retaddr,
55
-#define LO_COMMPAGE 0
153
- NotDirtyInfo *ndi)
56
+#define LO_COMMPAGE -1
154
+ TCGMemOpIdx oi, uintptr_t retaddr)
57
#elif defined(LO_COMMPAGE)
155
{
58
#define HI_COMMPAGE 0
156
size_t mmu_idx = get_mmuidx(oi);
59
#else
157
uintptr_t index = tlb_index(env, mmu_idx, addr);
60
#define HI_COMMPAGE 0
158
@@ -XXX,XX +XXX,XX @@ static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr,
61
-#define LO_COMMPAGE 0
159
62
+#define LO_COMMPAGE -1
160
hostaddr = (void *)((uintptr_t)addr + tlbe->addend);
63
#define init_guest_commpage() true
161
64
#endif
162
- ndi->active = false;
65
163
if (unlikely(tlb_addr & TLB_NOTDIRTY)) {
66
@@ -XXX,XX +XXX,XX @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
164
- ndi->active = true;
67
} else {
165
- memory_notdirty_write_prepare(ndi, env_cpu(env), addr,
68
offset = -(HI_COMMPAGE & -align);
166
- qemu_ram_addr_from_host_nofail(hostaddr),
69
}
167
- 1 << s_bits);
70
- } else if (LO_COMMPAGE != 0) {
168
+ notdirty_write(env_cpu(env), addr, 1 << s_bits,
71
+ } else if (LO_COMMPAGE != -1) {
169
+ &env_tlb(env)->d[mmu_idx].iotlb[index], retaddr);
72
loaddr = MIN(loaddr, LO_COMMPAGE & -align);
170
}
73
}
171
74
172
return hostaddr;
173
@@ -XXX,XX +XXX,XX @@ store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
174
return;
175
}
176
177
- haddr = (void *)((uintptr_t)addr + entry->addend);
178
-
179
/* Handle clean RAM pages. */
180
if (tlb_addr & TLB_NOTDIRTY) {
181
- NotDirtyInfo ndi;
182
-
183
- /* We require mem_io_pc in tb_invalidate_phys_page_range. */
184
- env_cpu(env)->mem_io_pc = retaddr;
185
-
186
- memory_notdirty_write_prepare(&ndi, env_cpu(env), addr,
187
- addr + iotlbentry->addr, size);
188
-
189
- if (unlikely(need_swap)) {
190
- store_memop(haddr, val, op ^ MO_BSWAP);
191
- } else {
192
- store_memop(haddr, val, op);
193
- }
194
-
195
- memory_notdirty_write_complete(&ndi);
196
- return;
197
+ notdirty_write(env_cpu(env), addr, size, iotlbentry, retaddr);
198
}
199
200
+ haddr = (void *)((uintptr_t)addr + entry->addend);
201
+
202
/*
203
* Keep these two store_memop separate to ensure that the compiler
204
* is able to fold the entire function to a single instruction.
205
@@ -XXX,XX +XXX,XX @@ void helper_be_stq_mmu(CPUArchState *env, target_ulong addr, uint64_t val,
206
#define EXTRA_ARGS , TCGMemOpIdx oi, uintptr_t retaddr
207
#define ATOMIC_NAME(X) \
208
HELPER(glue(glue(glue(atomic_ ## X, SUFFIX), END), _mmu))
209
-#define ATOMIC_MMU_DECLS NotDirtyInfo ndi
210
-#define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, oi, retaddr, &ndi)
211
-#define ATOMIC_MMU_CLEANUP \
212
- do { \
213
- if (unlikely(ndi.active)) { \
214
- memory_notdirty_write_complete(&ndi); \
215
- } \
216
- } while (0)
217
+#define ATOMIC_MMU_DECLS
218
+#define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, oi, retaddr)
219
+#define ATOMIC_MMU_CLEANUP
220
221
#define DATA_SIZE 1
222
#include "atomic_template.h"
223
@@ -XXX,XX +XXX,XX @@ void helper_be_stq_mmu(CPUArchState *env, target_ulong addr, uint64_t val,
224
#undef ATOMIC_MMU_LOOKUP
225
#define EXTRA_ARGS , TCGMemOpIdx oi
226
#define ATOMIC_NAME(X) HELPER(glue(glue(atomic_ ## X, SUFFIX), END))
227
-#define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, oi, GETPC(), &ndi)
228
+#define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, oi, GETPC())
229
230
#define DATA_SIZE 1
231
#include "atomic_template.h"
232
diff --git a/exec.c b/exec.c
233
index XXXXXXX..XXXXXXX 100644
234
--- a/exec.c
235
+++ b/exec.c
236
@@ -XXX,XX +XXX,XX @@ ram_addr_t qemu_ram_addr_from_host(void *ptr)
237
return block->offset + offset;
238
}
239
240
-/* Called within RCU critical section. */
241
-void memory_notdirty_write_prepare(NotDirtyInfo *ndi,
242
- CPUState *cpu,
243
- vaddr mem_vaddr,
244
- ram_addr_t ram_addr,
245
- unsigned size)
246
-{
247
- ndi->cpu = cpu;
248
- ndi->ram_addr = ram_addr;
249
- ndi->mem_vaddr = mem_vaddr;
250
- ndi->size = size;
251
- ndi->pages = NULL;
252
-
253
- trace_memory_notdirty_write_access(mem_vaddr, ram_addr, size);
254
-
255
- assert(tcg_enabled());
256
- if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) {
257
- ndi->pages = page_collection_lock(ram_addr, ram_addr + size);
258
- tb_invalidate_phys_page_fast(ndi->pages, ram_addr, size);
259
- }
260
-}
261
-
262
-/* Called within RCU critical section. */
263
-void memory_notdirty_write_complete(NotDirtyInfo *ndi)
264
-{
265
- if (ndi->pages) {
266
- assert(tcg_enabled());
267
- page_collection_unlock(ndi->pages);
268
- ndi->pages = NULL;
269
- }
270
-
271
- /* Set both VGA and migration bits for simplicity and to remove
272
- * the notdirty callback faster.
273
- */
274
- cpu_physical_memory_set_dirty_range(ndi->ram_addr, ndi->size,
275
- DIRTY_CLIENTS_NOCODE);
276
- /* we remove the notdirty callback only if the code has been
277
- flushed */
278
- if (!cpu_physical_memory_is_clean(ndi->ram_addr)) {
279
- trace_memory_notdirty_set_dirty(ndi->mem_vaddr);
280
- tlb_set_dirty(ndi->cpu, ndi->mem_vaddr);
281
- }
282
-}
283
-
284
/* Generate a debug exception if a watchpoint has been hit. */
285
void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len,
286
MemTxAttrs attrs, int flags, uintptr_t ra)
287
--
75
--
288
2.17.1
76
2.34.1
289
290
diff view generated by jsdifflib
New patch
1
We're about to start validating PAGE_EXEC, which means that we've
2
got to mark the vsyscall page executable. We had been special
3
casing this entirely within translate.
1
4
5
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
6
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
7
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
8
---
9
linux-user/elfload.c | 23 +++++++++++++++++++++++
10
1 file changed, 23 insertions(+)
11
12
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
13
index XXXXXXX..XXXXXXX 100644
14
--- a/linux-user/elfload.c
15
+++ b/linux-user/elfload.c
16
@@ -XXX,XX +XXX,XX @@ static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *en
17
(*regs)[26] = tswapreg(env->segs[R_GS].selector & 0xffff);
18
}
19
20
+#if ULONG_MAX >= TARGET_VSYSCALL_PAGE
21
+#define INIT_GUEST_COMMPAGE
22
+static bool init_guest_commpage(void)
23
+{
24
+ /*
25
+ * The vsyscall page is at a high negative address aka kernel space,
26
+ * which means that we cannot actually allocate it with target_mmap.
27
+ * We still should be able to use page_set_flags, unless the user
28
+ * has specified -R reserved_va, which would trigger an assert().
29
+ */
30
+ if (reserved_va != 0 &&
31
+ TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE >= reserved_va) {
32
+ error_report("Cannot allocate vsyscall page");
33
+ exit(EXIT_FAILURE);
34
+ }
35
+ page_set_flags(TARGET_VSYSCALL_PAGE,
36
+ TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE,
37
+ PAGE_EXEC | PAGE_VALID);
38
+ return true;
39
+}
40
+#endif
41
#else
42
43
#define ELF_START_MMAP 0x80000000
44
@@ -XXX,XX +XXX,XX @@ static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
45
#else
46
#define HI_COMMPAGE 0
47
#define LO_COMMPAGE -1
48
+#ifndef INIT_GUEST_COMMPAGE
49
#define init_guest_commpage() true
50
#endif
51
+#endif
52
53
static void pgb_fail_in_use(const char *image_name)
54
{
55
--
56
2.34.1
diff view generated by jsdifflib
1
Use this as a compile-time assert that a particular
1
We cannot deliver two interrupts simultaneously;
2
code path is not reachable.
2
the first interrupt handler must execute first.
3
3
4
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
4
Reviewed-by: Michael Rolnik <mrolnik@gmail.com>
5
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
5
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
6
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
6
---
7
---
7
include/qemu/compiler.h | 15 +++++++++++++++
8
target/avr/helper.c | 9 +++------
8
1 file changed, 15 insertions(+)
9
1 file changed, 3 insertions(+), 6 deletions(-)
9
10
10
diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h
11
diff --git a/target/avr/helper.c b/target/avr/helper.c
11
index XXXXXXX..XXXXXXX 100644
12
index XXXXXXX..XXXXXXX 100644
12
--- a/include/qemu/compiler.h
13
--- a/target/avr/helper.c
13
+++ b/include/qemu/compiler.h
14
+++ b/target/avr/helper.c
14
@@ -XXX,XX +XXX,XX @@
15
@@ -XXX,XX +XXX,XX @@
15
#define QEMU_GENERIC9(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, __VA_ARGS__))
16
16
#define QEMU_GENERIC10(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x, __VA_ARGS__))
17
bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
17
18
{
18
+/**
19
- bool ret = false;
19
+ * qemu_build_not_reached()
20
AVRCPU *cpu = AVR_CPU(cs);
20
+ *
21
CPUAVRState *env = &cpu->env;
21
+ * The compiler, during optimization, is expected to prove that a call
22
22
+ * to this function cannot be reached and remove it. If the compiler
23
@@ -XXX,XX +XXX,XX @@ bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
23
+ * supports QEMU_ERROR, this will be reported at compile time; otherwise
24
avr_cpu_do_interrupt(cs);
24
+ * this will be reported at link time due to the missing symbol.
25
25
+ */
26
cs->interrupt_request &= ~CPU_INTERRUPT_RESET;
26
+#ifdef __OPTIMIZE__
27
-
27
+extern void QEMU_NORETURN QEMU_ERROR("code path is reachable")
28
- ret = true;
28
+ qemu_build_not_reached(void);
29
+ return true;
29
+#else
30
}
30
+#define qemu_build_not_reached() g_assert_not_reached()
31
}
31
+#endif
32
if (interrupt_request & CPU_INTERRUPT_HARD) {
32
+
33
@@ -XXX,XX +XXX,XX @@ bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
33
#endif /* COMPILER_H */
34
if (!env->intsrc) {
35
cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
36
}
37
-
38
- ret = true;
39
+ return true;
40
}
41
}
42
- return ret;
43
+ return false;
44
}
45
46
void avr_cpu_do_interrupt(CPUState *cs)
34
--
47
--
35
2.17.1
48
2.34.1
36
49
37
50
diff view generated by jsdifflib
1
With the merge of notdirty handling into store_helper,
1
This bit is not saved across interrupts, so we must
2
the last user of cpu->mem_io_vaddr was removed.
2
delay delivering the interrupt until the skip has
3
been processed.
3
4
4
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
5
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1118
5
Reviewed-by: David Hildenbrand <david@redhat.com>
6
Reviewed-by: Michael Rolnik <mrolnik@gmail.com>
7
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
6
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
8
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
7
---
9
---
8
include/hw/core/cpu.h | 2 --
10
target/avr/helper.c | 9 +++++++++
9
accel/tcg/cputlb.c | 2 --
11
target/avr/translate.c | 26 ++++++++++++++++++++++----
10
hw/core/cpu.c | 1 -
12
2 files changed, 31 insertions(+), 4 deletions(-)
11
3 files changed, 5 deletions(-)
12
13
13
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
14
diff --git a/target/avr/helper.c b/target/avr/helper.c
14
index XXXXXXX..XXXXXXX 100644
15
index XXXXXXX..XXXXXXX 100644
15
--- a/include/hw/core/cpu.h
16
--- a/target/avr/helper.c
16
+++ b/include/hw/core/cpu.h
17
+++ b/target/avr/helper.c
17
@@ -XXX,XX +XXX,XX @@ struct qemu_work_item;
18
@@ -XXX,XX +XXX,XX @@ bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
18
* @next_cpu: Next CPU sharing TB cache.
19
AVRCPU *cpu = AVR_CPU(cs);
19
* @opaque: User data.
20
CPUAVRState *env = &cpu->env;
20
* @mem_io_pc: Host Program Counter at which the memory was accessed.
21
21
- * @mem_io_vaddr: Target virtual address at which the memory was accessed.
22
+ /*
22
* @kvm_fd: vCPU file descriptor for KVM.
23
+ * We cannot separate a skip from the next instruction,
23
* @work_mutex: Lock to prevent multiple access to queued_work_*.
24
+ * as the skip would not be preserved across the interrupt.
24
* @queued_work_first: First asynchronous work pending.
25
+ * Separating the two insn normally only happens at page boundaries.
25
@@ -XXX,XX +XXX,XX @@ struct CPUState {
26
+ */
26
* we store some rarely used information in the CPU context.
27
+ if (env->skip) {
27
*/
28
+ return false;
28
uintptr_t mem_io_pc;
29
+ }
29
- vaddr mem_io_vaddr;
30
+
30
/*
31
if (interrupt_request & CPU_INTERRUPT_RESET) {
31
* This is only needed for the legacy cpu_unassigned_access() hook;
32
if (cpu_interrupts_enabled(env)) {
32
* when all targets using it have been converted to use
33
cs->exception_index = EXCP_RESET;
33
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
34
diff --git a/target/avr/translate.c b/target/avr/translate.c
34
index XXXXXXX..XXXXXXX 100644
35
index XXXXXXX..XXXXXXX 100644
35
--- a/accel/tcg/cputlb.c
36
--- a/target/avr/translate.c
36
+++ b/accel/tcg/cputlb.c
37
+++ b/target/avr/translate.c
37
@@ -XXX,XX +XXX,XX @@ static uint64_t io_readx(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
38
@@ -XXX,XX +XXX,XX @@ static void avr_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
38
cpu_io_recompile(cpu, retaddr);
39
if (skip_label) {
40
canonicalize_skip(ctx);
41
gen_set_label(skip_label);
42
- if (ctx->base.is_jmp == DISAS_NORETURN) {
43
+
44
+ switch (ctx->base.is_jmp) {
45
+ case DISAS_NORETURN:
46
ctx->base.is_jmp = DISAS_CHAIN;
47
+ break;
48
+ case DISAS_NEXT:
49
+ if (ctx->base.tb->flags & TB_FLAGS_SKIP) {
50
+ ctx->base.is_jmp = DISAS_TOO_MANY;
51
+ }
52
+ break;
53
+ default:
54
+ break;
55
}
39
}
56
}
40
57
41
- cpu->mem_io_vaddr = addr;
58
@@ -XXX,XX +XXX,XX @@ static void avr_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
42
cpu->mem_io_access_type = access_type;
59
{
43
60
DisasContext *ctx = container_of(dcbase, DisasContext, base);
44
if (mr->global_locking && !qemu_mutex_iothread_locked()) {
61
bool nonconst_skip = canonicalize_skip(ctx);
45
@@ -XXX,XX +XXX,XX @@ static void io_writex(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
62
+ /*
46
if (!cpu->can_do_io) {
63
+ * Because we disable interrupts while env->skip is set,
47
cpu_io_recompile(cpu, retaddr);
64
+ * we must return to the main loop to re-evaluate afterward.
48
}
65
+ */
49
- cpu->mem_io_vaddr = addr;
66
+ bool force_exit = ctx->base.tb->flags & TB_FLAGS_SKIP;
50
cpu->mem_io_pc = retaddr;
67
51
68
switch (ctx->base.is_jmp) {
52
if (mr->global_locking && !qemu_mutex_iothread_locked()) {
69
case DISAS_NORETURN:
53
diff --git a/hw/core/cpu.c b/hw/core/cpu.c
70
@@ -XXX,XX +XXX,XX @@ static void avr_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
54
index XXXXXXX..XXXXXXX 100644
71
case DISAS_NEXT:
55
--- a/hw/core/cpu.c
72
case DISAS_TOO_MANY:
56
+++ b/hw/core/cpu.c
73
case DISAS_CHAIN:
57
@@ -XXX,XX +XXX,XX @@ static void cpu_common_reset(CPUState *cpu)
74
- if (!nonconst_skip) {
58
cpu->interrupt_request = 0;
75
+ if (!nonconst_skip && !force_exit) {
59
cpu->halted = 0;
76
/* Note gen_goto_tb checks singlestep. */
60
cpu->mem_io_pc = 0;
77
gen_goto_tb(ctx, 1, ctx->npc);
61
- cpu->mem_io_vaddr = 0;
78
break;
62
cpu->icount_extra = 0;
79
@@ -XXX,XX +XXX,XX @@ static void avr_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
63
atomic_set(&cpu->icount_decr_ptr->u32, 0);
80
tcg_gen_movi_tl(cpu_pc, ctx->npc);
64
cpu->can_do_io = 1;
81
/* fall through */
82
case DISAS_LOOKUP:
83
- tcg_gen_lookup_and_goto_ptr();
84
- break;
85
+ if (!force_exit) {
86
+ tcg_gen_lookup_and_goto_ptr();
87
+ break;
88
+ }
89
+ /* fall through */
90
case DISAS_EXIT:
91
tcg_gen_exit_tb(NULL, 0);
92
break;
65
--
93
--
66
2.17.1
94
2.34.1
67
95
68
96
diff view generated by jsdifflib
1
We can use notdirty_write for the write and return a valid host
1
Map the stack executable if required by default or on demand.
2
pointer for this case.
3
2
4
Reviewed-by: David Hildenbrand <david@redhat.com>
3
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
5
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
4
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
6
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
5
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
7
---
6
---
8
accel/tcg/cputlb.c | 26 +++++++++++++++++---------
7
include/elf.h | 1 +
9
1 file changed, 17 insertions(+), 9 deletions(-)
8
linux-user/qemu.h | 1 +
9
linux-user/elfload.c | 19 ++++++++++++++++++-
10
3 files changed, 20 insertions(+), 1 deletion(-)
10
11
11
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
12
diff --git a/include/elf.h b/include/elf.h
12
index XXXXXXX..XXXXXXX 100644
13
index XXXXXXX..XXXXXXX 100644
13
--- a/accel/tcg/cputlb.c
14
--- a/include/elf.h
14
+++ b/accel/tcg/cputlb.c
15
+++ b/include/elf.h
15
@@ -XXX,XX +XXX,XX @@ void *probe_access(CPUArchState *env, target_ulong addr, int size,
16
@@ -XXX,XX +XXX,XX @@ typedef int64_t Elf64_Sxword;
16
return NULL;
17
#define PT_LOPROC 0x70000000
18
#define PT_HIPROC 0x7fffffff
19
20
+#define PT_GNU_STACK (PT_LOOS + 0x474e551)
21
#define PT_GNU_PROPERTY (PT_LOOS + 0x474e553)
22
23
#define PT_MIPS_REGINFO 0x70000000
24
diff --git a/linux-user/qemu.h b/linux-user/qemu.h
25
index XXXXXXX..XXXXXXX 100644
26
--- a/linux-user/qemu.h
27
+++ b/linux-user/qemu.h
28
@@ -XXX,XX +XXX,XX @@ struct image_info {
29
uint32_t elf_flags;
30
int personality;
31
abi_ulong alignment;
32
+ bool exec_stack;
33
34
/* Generic semihosting knows about these pointers. */
35
abi_ulong arg_strings; /* strings for argv */
36
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
37
index XXXXXXX..XXXXXXX 100644
38
--- a/linux-user/elfload.c
39
+++ b/linux-user/elfload.c
40
@@ -XXX,XX +XXX,XX @@ static bool init_guest_commpage(void)
41
#define ELF_ARCH EM_386
42
43
#define ELF_PLATFORM get_elf_platform()
44
+#define EXSTACK_DEFAULT true
45
46
static const char *get_elf_platform(void)
47
{
48
@@ -XXX,XX +XXX,XX @@ static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *en
49
50
#define ELF_ARCH EM_ARM
51
#define ELF_CLASS ELFCLASS32
52
+#define EXSTACK_DEFAULT true
53
54
static inline void init_thread(struct target_pt_regs *regs,
55
struct image_info *infop)
56
@@ -XXX,XX +XXX,XX @@ static inline void init_thread(struct target_pt_regs *regs,
57
#else
58
59
#define ELF_CLASS ELFCLASS32
60
+#define EXSTACK_DEFAULT true
61
62
#endif
63
64
@@ -XXX,XX +XXX,XX @@ static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *en
65
66
#define ELF_CLASS ELFCLASS64
67
#define ELF_ARCH EM_LOONGARCH
68
+#define EXSTACK_DEFAULT true
69
70
#define elf_check_arch(x) ((x) == EM_LOONGARCH)
71
72
@@ -XXX,XX +XXX,XX @@ static uint32_t get_elf_hwcap(void)
73
#define ELF_CLASS ELFCLASS32
74
#endif
75
#define ELF_ARCH EM_MIPS
76
+#define EXSTACK_DEFAULT true
77
78
#ifdef TARGET_ABI_MIPSN32
79
#define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
80
@@ -XXX,XX +XXX,XX @@ static inline void init_thread(struct target_pt_regs *regs,
81
#define bswaptls(ptr) bswap32s(ptr)
82
#endif
83
84
+#ifndef EXSTACK_DEFAULT
85
+#define EXSTACK_DEFAULT false
86
+#endif
87
+
88
#include "elf.h"
89
90
/* We must delay the following stanzas until after "elf.h". */
91
@@ -XXX,XX +XXX,XX @@ static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
92
struct image_info *info)
93
{
94
abi_ulong size, error, guard;
95
+ int prot;
96
97
size = guest_stack_size;
98
if (size < STACK_LOWER_LIMIT) {
99
@@ -XXX,XX +XXX,XX @@ static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
100
guard = qemu_real_host_page_size();
17
}
101
}
18
102
19
- /* Handle watchpoints. */
103
- error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
20
- if (tlb_addr & TLB_WATCHPOINT) {
104
+ prot = PROT_READ | PROT_WRITE;
21
- cpu_check_watchpoint(env_cpu(env), addr, size,
105
+ if (info->exec_stack) {
22
- env_tlb(env)->d[mmu_idx].iotlb[index].attrs,
106
+ prot |= PROT_EXEC;
23
- wp_access, retaddr);
107
+ }
24
- }
108
+ error = target_mmap(0, size + guard, prot,
25
+ if (unlikely(tlb_addr & TLB_FLAGS_MASK)) {
109
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
26
+ CPUIOTLBEntry *iotlbentry = &env_tlb(env)->d[mmu_idx].iotlb[index];
110
if (error == -1) {
27
111
perror("mmap stack");
28
- /* Reject I/O access, or other required slow-path. */
112
@@ -XXX,XX +XXX,XX @@ static void load_elf_image(const char *image_name, int image_fd,
29
- if (tlb_addr & (TLB_NOTDIRTY | TLB_MMIO | TLB_BSWAP | TLB_DISCARD_WRITE)) {
113
*/
30
- return NULL;
114
loaddr = -1, hiaddr = 0;
31
+ /* Reject I/O access, or other required slow-path. */
115
info->alignment = 0;
32
+ if (tlb_addr & (TLB_MMIO | TLB_BSWAP | TLB_DISCARD_WRITE)) {
116
+ info->exec_stack = EXSTACK_DEFAULT;
33
+ return NULL;
117
for (i = 0; i < ehdr->e_phnum; ++i) {
34
+ }
118
struct elf_phdr *eppnt = phdr + i;
35
+
119
if (eppnt->p_type == PT_LOAD) {
36
+ /* Handle watchpoints. */
120
@@ -XXX,XX +XXX,XX @@ static void load_elf_image(const char *image_name, int image_fd,
37
+ if (tlb_addr & TLB_WATCHPOINT) {
121
if (!parse_elf_properties(image_fd, info, eppnt, bprm_buf, &err)) {
38
+ cpu_check_watchpoint(env_cpu(env), addr, size,
122
goto exit_errmsg;
39
+ iotlbentry->attrs, wp_access, retaddr);
123
}
40
+ }
124
+ } else if (eppnt->p_type == PT_GNU_STACK) {
41
+
125
+ info->exec_stack = eppnt->p_flags & PF_X;
42
+ /* Handle clean RAM pages. */
126
}
43
+ if (tlb_addr & TLB_NOTDIRTY) {
44
+ notdirty_write(env_cpu(env), addr, size, iotlbentry, retaddr);
45
+ }
46
}
127
}
47
128
48
return (void *)((uintptr_t)addr + entry->addend);
49
--
129
--
50
2.17.1
130
2.34.1
51
52
diff view generated by jsdifflib
New patch
1
From: Ilya Leoshkevich <iii@linux.ibm.com>
1
2
3
Currently it's possible to execute pages that do not have PAGE_EXEC
4
if there is an existing translation block. Fix by invalidating TBs
5
that touch the affected pages.
6
7
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
8
Message-Id: <20220817150506.592862-2-iii@linux.ibm.com>
9
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
10
---
11
linux-user/mmap.c | 6 ++++--
12
1 file changed, 4 insertions(+), 2 deletions(-)
13
14
diff --git a/linux-user/mmap.c b/linux-user/mmap.c
15
index XXXXXXX..XXXXXXX 100644
16
--- a/linux-user/mmap.c
17
+++ b/linux-user/mmap.c
18
@@ -XXX,XX +XXX,XX @@ int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
19
goto error;
20
}
21
}
22
+
23
page_set_flags(start, start + len, page_flags);
24
- mmap_unlock();
25
- return 0;
26
+ tb_invalidate_phys_range(start, start + len);
27
+ ret = 0;
28
+
29
error:
30
mmap_unlock();
31
return ret;
32
--
33
2.34.1
diff view generated by jsdifflib
New patch
1
We're about to start validating PAGE_EXEC, which means
2
that we've got to put this code into a section that is
3
both writable and executable.
1
4
5
Note that this test did not run on hardware beforehand either.
6
7
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
8
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
9
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
10
---
11
tests/tcg/i386/test-i386.c | 2 +-
12
1 file changed, 1 insertion(+), 1 deletion(-)
13
14
diff --git a/tests/tcg/i386/test-i386.c b/tests/tcg/i386/test-i386.c
15
index XXXXXXX..XXXXXXX 100644
16
--- a/tests/tcg/i386/test-i386.c
17
+++ b/tests/tcg/i386/test-i386.c
18
@@ -XXX,XX +XXX,XX @@ uint8_t code[] = {
19
0xc3, /* ret */
20
};
21
22
-asm(".section \".data\"\n"
23
+asm(".section \".data_x\",\"awx\"\n"
24
"smc_code2:\n"
25
"movl 4(%esp), %eax\n"
26
"movl %eax, smc_patch_addr2 + 1\n"
27
--
28
2.34.1
diff view generated by jsdifflib
1
This forced inlining can result in missing symbols,
1
From: Ilya Leoshkevich <iii@linux.ibm.com>
2
which makes a debugging build harder to follow.
3
2
4
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
3
Introduce a function that checks whether a given address is on the same
5
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
4
page as where disassembly started. Having it improves readability of
6
Reviewed-by: David Hildenbrand <david@redhat.com>
5
the following patches.
7
Reported-by: Peter Maydell <peter.maydell@linaro.org>
6
7
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
8
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
9
Message-Id: <20220811095534.241224-3-iii@linux.ibm.com>
10
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
11
[rth: Make the DisasContextBase parameter const.]
8
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
12
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
9
---
13
---
10
include/qemu/compiler.h | 11 +++++++++++
14
include/exec/translator.h | 10 ++++++++++
11
accel/tcg/cputlb.c | 4 ++--
15
1 file changed, 10 insertions(+)
12
2 files changed, 13 insertions(+), 2 deletions(-)
13
16
14
diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h
17
diff --git a/include/exec/translator.h b/include/exec/translator.h
15
index XXXXXXX..XXXXXXX 100644
18
index XXXXXXX..XXXXXXX 100644
16
--- a/include/qemu/compiler.h
19
--- a/include/exec/translator.h
17
+++ b/include/qemu/compiler.h
20
+++ b/include/exec/translator.h
18
@@ -XXX,XX +XXX,XX @@
21
@@ -XXX,XX +XXX,XX @@ FOR_EACH_TRANSLATOR_LD(GEN_TRANSLATOR_LD)
19
# define QEMU_NONSTRING
22
20
#endif
23
#undef GEN_TRANSLATOR_LD
21
24
22
+/*
25
+/*
23
+ * Forced inlining may be desired to encourage constant propagation
26
+ * Return whether addr is on the same page as where disassembly started.
24
+ * of function parameters. However, it can also make debugging harder,
27
+ * Translators can use this to enforce the rule that only single-insn
25
+ * so disable it for a non-optimizing build.
28
+ * translation blocks are allowed to cross page boundaries.
26
+ */
29
+ */
27
+#if defined(__OPTIMIZE__)
30
+static inline bool is_same_page(const DisasContextBase *db, target_ulong addr)
28
+#define QEMU_ALWAYS_INLINE __attribute__((always_inline))
31
+{
29
+#else
32
+ return ((addr ^ db->pc_first) & TARGET_PAGE_MASK) == 0;
30
+#define QEMU_ALWAYS_INLINE
33
+}
31
+#endif
32
+
34
+
33
/* Implement C11 _Generic via GCC builtins. Example:
35
#endif /* EXEC__TRANSLATOR_H */
34
*
35
* QEMU_GENERIC(x, (float, sinf), (long double, sinl), sin) (x)
36
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
37
index XXXXXXX..XXXXXXX 100644
38
--- a/accel/tcg/cputlb.c
39
+++ b/accel/tcg/cputlb.c
40
@@ -XXX,XX +XXX,XX @@ static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr,
41
typedef uint64_t FullLoadHelper(CPUArchState *env, target_ulong addr,
42
TCGMemOpIdx oi, uintptr_t retaddr);
43
44
-static inline uint64_t __attribute__((always_inline))
45
+static inline uint64_t QEMU_ALWAYS_INLINE
46
load_helper(CPUArchState *env, target_ulong addr, TCGMemOpIdx oi,
47
uintptr_t retaddr, MemOp op, bool code_read,
48
FullLoadHelper *full_load)
49
@@ -XXX,XX +XXX,XX @@ tcg_target_ulong helper_be_ldsl_mmu(CPUArchState *env, target_ulong addr,
50
* Store Helpers
51
*/
52
53
-static inline void __attribute__((always_inline))
54
+static inline void QEMU_ALWAYS_INLINE
55
store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
56
TCGMemOpIdx oi, uintptr_t retaddr, MemOp op)
57
{
58
--
36
--
59
2.17.1
37
2.34.1
60
61
diff view generated by jsdifflib
1
It does not require going through the whole I/O path
1
The current implementation is a no-op, simply returning addr.
2
in order to discard a write.
2
This is incorrect, because we ought to be checking the page
3
permissions for execution.
3
4
4
Reviewed-by: David Hildenbrand <david@redhat.com>
5
Make get_page_addr_code inline for both implementations.
6
7
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
8
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
9
Acked-by: Alistair Francis <alistair.francis@wdc.com>
5
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
10
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
6
---
11
---
7
include/exec/cpu-all.h | 5 ++++-
12
include/exec/exec-all.h | 85 ++++++++++++++---------------------------
8
include/exec/cpu-common.h | 1 -
13
accel/tcg/cputlb.c | 5 ---
9
accel/tcg/cputlb.c | 36 ++++++++++++++++++++--------------
14
accel/tcg/user-exec.c | 14 +++++++
10
exec.c | 41 +--------------------------------------
15
3 files changed, 42 insertions(+), 62 deletions(-)
11
4 files changed, 26 insertions(+), 57 deletions(-)
12
16
13
diff --git a/include/exec/cpu-all.h b/include/exec/cpu-all.h
17
diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h
14
index XXXXXXX..XXXXXXX 100644
18
index XXXXXXX..XXXXXXX 100644
15
--- a/include/exec/cpu-all.h
19
--- a/include/exec/exec-all.h
16
+++ b/include/exec/cpu-all.h
20
+++ b/include/exec/exec-all.h
17
@@ -XXX,XX +XXX,XX @@ CPUArchState *cpu_copy(CPUArchState *env);
21
@@ -XXX,XX +XXX,XX @@ struct MemoryRegionSection *iotlb_to_section(CPUState *cpu,
18
#define TLB_WATCHPOINT (1 << (TARGET_PAGE_BITS_MIN - 4))
22
hwaddr index, MemTxAttrs attrs);
19
/* Set if TLB entry requires byte swap. */
23
#endif
20
#define TLB_BSWAP (1 << (TARGET_PAGE_BITS_MIN - 5))
24
21
+/* Set if TLB entry writes ignored. */
25
-#if defined(CONFIG_USER_ONLY)
22
+#define TLB_DISCARD_WRITE (1 << (TARGET_PAGE_BITS_MIN - 6))
26
-void mmap_lock(void);
23
27
-void mmap_unlock(void);
24
/* Use this mask to check interception with an alignment mask
28
-bool have_mmap_lock(void);
25
* in a TCG backend.
29
-
30
/**
31
- * get_page_addr_code() - user-mode version
32
+ * get_page_addr_code_hostp()
33
* @env: CPUArchState
34
* @addr: guest virtual address of guest code
35
*
36
- * Returns @addr.
37
+ * See get_page_addr_code() (full-system version) for documentation on the
38
+ * return value.
39
+ *
40
+ * Sets *@hostp (when @hostp is non-NULL) as follows.
41
+ * If the return value is -1, sets *@hostp to NULL. Otherwise, sets *@hostp
42
+ * to the host address where @addr's content is kept.
43
+ *
44
+ * Note: this function can trigger an exception.
45
+ */
46
+tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr,
47
+ void **hostp);
48
+
49
+/**
50
+ * get_page_addr_code()
51
+ * @env: CPUArchState
52
+ * @addr: guest virtual address of guest code
53
+ *
54
+ * If we cannot translate and execute from the entire RAM page, or if
55
+ * the region is not backed by RAM, returns -1. Otherwise, returns the
56
+ * ram_addr_t corresponding to the guest code at @addr.
57
+ *
58
+ * Note: this function can trigger an exception.
26
*/
59
*/
27
#define TLB_FLAGS_MASK \
60
static inline tb_page_addr_t get_page_addr_code(CPUArchState *env,
28
- (TLB_INVALID_MASK | TLB_NOTDIRTY | TLB_MMIO | TLB_WATCHPOINT | TLB_BSWAP)
61
target_ulong addr)
29
+ (TLB_INVALID_MASK | TLB_NOTDIRTY | TLB_MMIO \
62
{
30
+ | TLB_WATCHPOINT | TLB_BSWAP | TLB_DISCARD_WRITE)
63
- return addr;
64
+ return get_page_addr_code_hostp(env, addr, NULL);
65
}
66
67
-/**
68
- * get_page_addr_code_hostp() - user-mode version
69
- * @env: CPUArchState
70
- * @addr: guest virtual address of guest code
71
- *
72
- * Returns @addr.
73
- *
74
- * If @hostp is non-NULL, sets *@hostp to the host address where @addr's content
75
- * is kept.
76
- */
77
-static inline tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env,
78
- target_ulong addr,
79
- void **hostp)
80
-{
81
- if (hostp) {
82
- *hostp = g2h_untagged(addr);
83
- }
84
- return addr;
85
-}
86
+#if defined(CONFIG_USER_ONLY)
87
+void mmap_lock(void);
88
+void mmap_unlock(void);
89
+bool have_mmap_lock(void);
31
90
32
/**
91
/**
33
* tlb_hit_page: return true if page aligned @addr is a hit against the
92
* adjust_signal_pc:
34
diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h
93
@@ -XXX,XX +XXX,XX @@ G_NORETURN void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
35
index XXXXXXX..XXXXXXX 100644
94
static inline void mmap_lock(void) {}
36
--- a/include/exec/cpu-common.h
95
static inline void mmap_unlock(void) {}
37
+++ b/include/exec/cpu-common.h
96
38
@@ -XXX,XX +XXX,XX @@ void qemu_flush_coalesced_mmio_buffer(void);
97
-/**
39
98
- * get_page_addr_code() - full-system version
40
void cpu_flush_icache_range(hwaddr start, hwaddr len);
99
- * @env: CPUArchState
41
100
- * @addr: guest virtual address of guest code
42
-extern struct MemoryRegion io_mem_rom;
101
- *
43
extern struct MemoryRegion io_mem_notdirty;
102
- * If we cannot translate and execute from the entire RAM page, or if
44
103
- * the region is not backed by RAM, returns -1. Otherwise, returns the
45
typedef int (RAMBlockIterFunc)(RAMBlock *rb, void *opaque);
104
- * ram_addr_t corresponding to the guest code at @addr.
105
- *
106
- * Note: this function can trigger an exception.
107
- */
108
-tb_page_addr_t get_page_addr_code(CPUArchState *env, target_ulong addr);
109
-
110
-/**
111
- * get_page_addr_code_hostp() - full-system version
112
- * @env: CPUArchState
113
- * @addr: guest virtual address of guest code
114
- *
115
- * See get_page_addr_code() (full-system version) for documentation on the
116
- * return value.
117
- *
118
- * Sets *@hostp (when @hostp is non-NULL) as follows.
119
- * If the return value is -1, sets *@hostp to NULL. Otherwise, sets *@hostp
120
- * to the host address where @addr's content is kept.
121
- *
122
- * Note: this function can trigger an exception.
123
- */
124
-tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr,
125
- void **hostp);
126
-
127
void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length);
128
void tlb_set_dirty(CPUState *cpu, target_ulong vaddr);
129
46
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
130
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
47
index XXXXXXX..XXXXXXX 100644
131
index XXXXXXX..XXXXXXX 100644
48
--- a/accel/tcg/cputlb.c
132
--- a/accel/tcg/cputlb.c
49
+++ b/accel/tcg/cputlb.c
133
+++ b/accel/tcg/cputlb.c
50
@@ -XXX,XX +XXX,XX @@ static void tlb_reset_dirty_range_locked(CPUTLBEntry *tlb_entry,
134
@@ -XXX,XX +XXX,XX @@ tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr,
51
{
135
return qemu_ram_addr_from_host_nofail(p);
52
uintptr_t addr = tlb_entry->addr_write;
53
54
- if ((addr & (TLB_INVALID_MASK | TLB_MMIO | TLB_NOTDIRTY)) == 0) {
55
+ if ((addr & (TLB_INVALID_MASK | TLB_MMIO |
56
+ TLB_DISCARD_WRITE | TLB_NOTDIRTY)) == 0) {
57
addr &= TARGET_PAGE_MASK;
58
addr += tlb_entry->addend;
59
if ((addr - start) < length) {
60
@@ -XXX,XX +XXX,XX @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
61
address |= TLB_MMIO;
62
addend = 0;
63
} else {
64
- /* TLB_MMIO for rom/romd handled below */
65
addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat;
66
}
67
68
@@ -XXX,XX +XXX,XX @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
69
70
tn.addr_write = -1;
71
if (prot & PAGE_WRITE) {
72
- if ((memory_region_is_ram(section->mr) && section->readonly)
73
- || memory_region_is_romd(section->mr)) {
74
- /* Write access calls the I/O callback. */
75
- tn.addr_write = address | TLB_MMIO;
76
- } else if (memory_region_is_ram(section->mr)
77
- && cpu_physical_memory_is_clean(
78
- memory_region_get_ram_addr(section->mr) + xlat)) {
79
- tn.addr_write = address | TLB_NOTDIRTY;
80
- } else {
81
- tn.addr_write = address;
82
+ tn.addr_write = address;
83
+ if (memory_region_is_romd(section->mr)) {
84
+ /* Use the MMIO path so that the device can switch states. */
85
+ tn.addr_write |= TLB_MMIO;
86
+ } else if (memory_region_is_ram(section->mr)) {
87
+ if (section->readonly) {
88
+ tn.addr_write |= TLB_DISCARD_WRITE;
89
+ } else if (cpu_physical_memory_is_clean(
90
+ memory_region_get_ram_addr(section->mr) + xlat)) {
91
+ tn.addr_write |= TLB_NOTDIRTY;
92
+ }
93
}
94
if (prot & PAGE_WRITE_INV) {
95
tn.addr_write |= TLB_INVALID_MASK;
96
@@ -XXX,XX +XXX,XX @@ static uint64_t io_readx(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
97
mr = section->mr;
98
mr_offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
99
cpu->mem_io_pc = retaddr;
100
- if (mr != &io_mem_rom && mr != &io_mem_notdirty && !cpu->can_do_io) {
101
+ if (mr != &io_mem_notdirty && !cpu->can_do_io) {
102
cpu_io_recompile(cpu, retaddr);
103
}
104
105
@@ -XXX,XX +XXX,XX @@ static void io_writex(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
106
section = iotlb_to_section(cpu, iotlbentry->addr, iotlbentry->attrs);
107
mr = section->mr;
108
mr_offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
109
- if (mr != &io_mem_rom && mr != &io_mem_notdirty && !cpu->can_do_io) {
110
+ if (mr != &io_mem_notdirty && !cpu->can_do_io) {
111
cpu_io_recompile(cpu, retaddr);
112
}
113
cpu->mem_io_vaddr = addr;
114
@@ -XXX,XX +XXX,XX @@ void *probe_access(CPUArchState *env, target_ulong addr, int size,
115
}
116
117
/* Reject I/O access, or other required slow-path. */
118
- if (tlb_addr & (TLB_NOTDIRTY | TLB_MMIO | TLB_BSWAP)) {
119
+ if (tlb_addr & (TLB_NOTDIRTY | TLB_MMIO | TLB_BSWAP | TLB_DISCARD_WRITE)) {
120
return NULL;
121
}
122
123
@@ -XXX,XX +XXX,XX @@ store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
124
return;
125
}
126
127
+ /* Ignore writes to ROM. */
128
+ if (unlikely(tlb_addr & TLB_DISCARD_WRITE)) {
129
+ return;
130
+ }
131
+
132
haddr = (void *)((uintptr_t)addr + entry->addend);
133
134
/*
135
diff --git a/exec.c b/exec.c
136
index XXXXXXX..XXXXXXX 100644
137
--- a/exec.c
138
+++ b/exec.c
139
@@ -XXX,XX +XXX,XX @@ static MemoryRegion *system_io;
140
AddressSpace address_space_io;
141
AddressSpace address_space_memory;
142
143
-MemoryRegion io_mem_rom, io_mem_notdirty;
144
+MemoryRegion io_mem_notdirty;
145
static MemoryRegion io_mem_unassigned;
146
#endif
147
148
@@ -XXX,XX +XXX,XX @@ typedef struct subpage_t {
149
150
#define PHYS_SECTION_UNASSIGNED 0
151
#define PHYS_SECTION_NOTDIRTY 1
152
-#define PHYS_SECTION_ROM 2
153
154
static void io_mem_init(void);
155
static void memory_map_init(void);
156
@@ -XXX,XX +XXX,XX @@ hwaddr memory_region_section_get_iotlb(CPUState *cpu,
157
iotlb = memory_region_get_ram_addr(section->mr) + xlat;
158
if (!section->readonly) {
159
iotlb |= PHYS_SECTION_NOTDIRTY;
160
- } else {
161
- iotlb |= PHYS_SECTION_ROM;
162
}
163
} else {
164
AddressSpaceDispatch *d;
165
@@ -XXX,XX +XXX,XX @@ static uint16_t dummy_section(PhysPageMap *map, FlatView *fv, MemoryRegion *mr)
166
return phys_section_add(map, &section);
167
}
136
}
168
137
169
-static void readonly_mem_write(void *opaque, hwaddr addr,
138
-tb_page_addr_t get_page_addr_code(CPUArchState *env, target_ulong addr)
170
- uint64_t val, unsigned size)
171
-{
139
-{
172
- /* Ignore any write to ROM. */
140
- return get_page_addr_code_hostp(env, addr, NULL);
173
-}
141
-}
174
-
142
-
175
-static bool readonly_mem_accepts(void *opaque, hwaddr addr,
143
static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size,
176
- unsigned size, bool is_write,
144
CPUIOTLBEntry *iotlbentry, uintptr_t retaddr)
177
- MemTxAttrs attrs)
178
-{
179
- return is_write;
180
-}
181
-
182
-/* This will only be used for writes, because reads are special cased
183
- * to directly access the underlying host ram.
184
- */
185
-static const MemoryRegionOps readonly_mem_ops = {
186
- .write = readonly_mem_write,
187
- .valid.accepts = readonly_mem_accepts,
188
- .endianness = DEVICE_NATIVE_ENDIAN,
189
- .valid = {
190
- .min_access_size = 1,
191
- .max_access_size = 8,
192
- .unaligned = false,
193
- },
194
- .impl = {
195
- .min_access_size = 1,
196
- .max_access_size = 8,
197
- .unaligned = false,
198
- },
199
-};
200
-
201
MemoryRegionSection *iotlb_to_section(CPUState *cpu,
202
hwaddr index, MemTxAttrs attrs)
203
{
145
{
204
@@ -XXX,XX +XXX,XX @@ MemoryRegionSection *iotlb_to_section(CPUState *cpu,
146
diff --git a/accel/tcg/user-exec.c b/accel/tcg/user-exec.c
205
147
index XXXXXXX..XXXXXXX 100644
206
static void io_mem_init(void)
148
--- a/accel/tcg/user-exec.c
207
{
149
+++ b/accel/tcg/user-exec.c
208
- memory_region_init_io(&io_mem_rom, NULL, &readonly_mem_ops,
150
@@ -XXX,XX +XXX,XX @@ void *probe_access(CPUArchState *env, target_ulong addr, int size,
209
- NULL, NULL, UINT64_MAX);
151
return size ? g2h(env_cpu(env), addr) : NULL;
210
memory_region_init_io(&io_mem_unassigned, NULL, &unassigned_mem_ops, NULL,
152
}
211
NULL, UINT64_MAX);
153
212
154
+tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr,
213
@@ -XXX,XX +XXX,XX @@ AddressSpaceDispatch *address_space_dispatch_new(FlatView *fv)
155
+ void **hostp)
214
assert(n == PHYS_SECTION_UNASSIGNED);
156
+{
215
n = dummy_section(&d->map, fv, &io_mem_notdirty);
157
+ int flags;
216
assert(n == PHYS_SECTION_NOTDIRTY);
158
+
217
- n = dummy_section(&d->map, fv, &io_mem_rom);
159
+ flags = probe_access_internal(env, addr, 1, MMU_INST_FETCH, false, 0);
218
- assert(n == PHYS_SECTION_ROM);
160
+ g_assert(flags == 0);
219
161
+
220
d->phys_map = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 };
162
+ if (hostp) {
221
163
+ *hostp = g2h_untagged(addr);
164
+ }
165
+ return addr;
166
+}
167
+
168
/* The softmmu versions of these helpers are in cputlb.c. */
169
170
/*
222
--
171
--
223
2.17.1
172
2.34.1
224
225
diff view generated by jsdifflib
New patch
1
The mmap_lock is held around tb_gen_code. While the comment
2
is correct that the lock is dropped when tb_gen_code runs out
3
of memory, the lock is *not* dropped when an exception is
4
raised reading code for translation.
1
5
6
Acked-by: Alistair Francis <alistair.francis@wdc.com>
7
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
8
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
9
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
10
---
11
accel/tcg/cpu-exec.c | 12 ++++++------
12
accel/tcg/user-exec.c | 3 ---
13
2 files changed, 6 insertions(+), 9 deletions(-)
14
15
diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c
16
index XXXXXXX..XXXXXXX 100644
17
--- a/accel/tcg/cpu-exec.c
18
+++ b/accel/tcg/cpu-exec.c
19
@@ -XXX,XX +XXX,XX @@ void cpu_exec_step_atomic(CPUState *cpu)
20
cpu_tb_exec(cpu, tb, &tb_exit);
21
cpu_exec_exit(cpu);
22
} else {
23
- /*
24
- * The mmap_lock is dropped by tb_gen_code if it runs out of
25
- * memory.
26
- */
27
#ifndef CONFIG_SOFTMMU
28
clear_helper_retaddr();
29
- tcg_debug_assert(!have_mmap_lock());
30
+ if (have_mmap_lock()) {
31
+ mmap_unlock();
32
+ }
33
#endif
34
if (qemu_mutex_iothread_locked()) {
35
qemu_mutex_unlock_iothread();
36
@@ -XXX,XX +XXX,XX @@ int cpu_exec(CPUState *cpu)
37
38
#ifndef CONFIG_SOFTMMU
39
clear_helper_retaddr();
40
- tcg_debug_assert(!have_mmap_lock());
41
+ if (have_mmap_lock()) {
42
+ mmap_unlock();
43
+ }
44
#endif
45
if (qemu_mutex_iothread_locked()) {
46
qemu_mutex_unlock_iothread();
47
diff --git a/accel/tcg/user-exec.c b/accel/tcg/user-exec.c
48
index XXXXXXX..XXXXXXX 100644
49
--- a/accel/tcg/user-exec.c
50
+++ b/accel/tcg/user-exec.c
51
@@ -XXX,XX +XXX,XX @@ MMUAccessType adjust_signal_pc(uintptr_t *pc, bool is_write)
52
* (and if the translator doesn't handle page boundaries correctly
53
* there's little we can do about that here). Therefore, do not
54
* trigger the unwinder.
55
- *
56
- * Like tb_gen_code, release the memory lock before cpu_loop_exit.
57
*/
58
- mmap_unlock();
59
*pc = 0;
60
return MMU_INST_FETCH;
61
}
62
--
63
2.34.1
diff view generated by jsdifflib
1
There is only one caller, tlb_set_page_with_attrs. We cannot
1
The function is not used outside of cpu-exec.c. Move it and
2
inline the entire function because the AddressSpaceDispatch
2
its subroutines up in the file, before the first use.
3
structure is private to exec.c, and cannot easily be moved to
4
include/exec/memory-internal.h.
5
3
6
Compute is_ram and is_romd once within tlb_set_page_with_attrs.
4
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
7
Fold the number of tests against these predicates. Compute
5
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
8
cpu_physical_memory_is_clean outside of the tlb lock region.
6
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
9
10
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
11
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
7
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
12
---
8
---
13
include/exec/exec-all.h | 6 +---
9
include/exec/exec-all.h | 3 -
14
accel/tcg/cputlb.c | 68 ++++++++++++++++++++++++++---------------
10
accel/tcg/cpu-exec.c | 122 ++++++++++++++++++++--------------------
15
exec.c | 22 ++-----------
11
2 files changed, 61 insertions(+), 64 deletions(-)
16
3 files changed, 47 insertions(+), 49 deletions(-)
17
12
18
diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h
13
diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h
19
index XXXXXXX..XXXXXXX 100644
14
index XXXXXXX..XXXXXXX 100644
20
--- a/include/exec/exec-all.h
15
--- a/include/exec/exec-all.h
21
+++ b/include/exec/exec-all.h
16
+++ b/include/exec/exec-all.h
22
@@ -XXX,XX +XXX,XX @@ address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
17
@@ -XXX,XX +XXX,XX @@ void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr, MemTxAttrs attrs);
23
hwaddr *xlat, hwaddr *plen,
24
MemTxAttrs attrs, int *prot);
25
hwaddr memory_region_section_get_iotlb(CPUState *cpu,
26
- MemoryRegionSection *section,
27
- target_ulong vaddr,
28
- hwaddr paddr, hwaddr xlat,
29
- int prot,
30
- target_ulong *address);
31
+ MemoryRegionSection *section);
32
#endif
18
#endif
33
19
void tb_flush(CPUState *cpu);
34
/* vl.c */
20
void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr);
35
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
21
-TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,
22
- target_ulong cs_base, uint32_t flags,
23
- uint32_t cflags);
24
void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr);
25
26
/* GETPC is the true target of the return instruction that we'll execute. */
27
diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c
36
index XXXXXXX..XXXXXXX 100644
28
index XXXXXXX..XXXXXXX 100644
37
--- a/accel/tcg/cputlb.c
29
--- a/accel/tcg/cpu-exec.c
38
+++ b/accel/tcg/cputlb.c
30
+++ b/accel/tcg/cpu-exec.c
39
@@ -XXX,XX +XXX,XX @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
31
@@ -XXX,XX +XXX,XX @@ uint32_t curr_cflags(CPUState *cpu)
40
MemoryRegionSection *section;
32
return cflags;
41
unsigned int index;
33
}
42
target_ulong address;
34
43
- target_ulong code_address;
35
+struct tb_desc {
44
+ target_ulong write_address;
36
+ target_ulong pc;
45
uintptr_t addend;
37
+ target_ulong cs_base;
46
CPUTLBEntry *te, tn;
38
+ CPUArchState *env;
47
hwaddr iotlb, xlat, sz, paddr_page;
39
+ tb_page_addr_t phys_page1;
48
target_ulong vaddr_page;
40
+ uint32_t flags;
49
int asidx = cpu_asidx_from_attrs(cpu, attrs);
41
+ uint32_t cflags;
50
int wp_flags;
42
+ uint32_t trace_vcpu_dstate;
51
+ bool is_ram, is_romd;
43
+};
52
53
assert_cpu_is_self(cpu);
54
55
@@ -XXX,XX +XXX,XX @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
56
if (attrs.byte_swap) {
57
address |= TLB_BSWAP;
58
}
59
- if (!memory_region_is_ram(section->mr) &&
60
- !memory_region_is_romd(section->mr)) {
61
- /* IO memory case */
62
- address |= TLB_MMIO;
63
- addend = 0;
64
- } else {
65
+
44
+
66
+ is_ram = memory_region_is_ram(section->mr);
45
+static bool tb_lookup_cmp(const void *p, const void *d)
67
+ is_romd = memory_region_is_romd(section->mr);
46
+{
47
+ const TranslationBlock *tb = p;
48
+ const struct tb_desc *desc = d;
68
+
49
+
69
+ if (is_ram || is_romd) {
50
+ if (tb->pc == desc->pc &&
70
+ /* RAM and ROMD both have associated host memory. */
51
+ tb->page_addr[0] == desc->phys_page1 &&
71
addend = (uintptr_t)memory_region_get_ram_ptr(section->mr) + xlat;
52
+ tb->cs_base == desc->cs_base &&
72
+ } else {
53
+ tb->flags == desc->flags &&
73
+ /* I/O does not; force the host address to NULL. */
54
+ tb->trace_vcpu_dstate == desc->trace_vcpu_dstate &&
74
+ addend = 0;
55
+ tb_cflags(tb) == desc->cflags) {
75
+ }
56
+ /* check next page if needed */
57
+ if (tb->page_addr[1] == -1) {
58
+ return true;
59
+ } else {
60
+ tb_page_addr_t phys_page2;
61
+ target_ulong virt_page2;
76
+
62
+
77
+ write_address = address;
63
+ virt_page2 = (desc->pc & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
78
+ if (is_ram) {
64
+ phys_page2 = get_page_addr_code(desc->env, virt_page2);
79
+ iotlb = memory_region_get_ram_addr(section->mr) + xlat;
65
+ if (tb->page_addr[1] == phys_page2) {
80
+ /*
66
+ return true;
81
+ * Computing is_clean is expensive; avoid all that unless
82
+ * the page is actually writable.
83
+ */
84
+ if (prot & PAGE_WRITE) {
85
+ if (section->readonly) {
86
+ write_address |= TLB_DISCARD_WRITE;
87
+ } else if (cpu_physical_memory_is_clean(iotlb)) {
88
+ write_address |= TLB_NOTDIRTY;
89
+ }
67
+ }
90
+ }
68
+ }
91
+ } else {
69
+ }
92
+ /* I/O or ROMD */
70
+ return false;
93
+ iotlb = memory_region_section_get_iotlb(cpu, section) + xlat;
71
+}
94
+ /*
72
+
95
+ * Writes to romd devices must go through MMIO to enable write.
73
+static TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,
96
+ * Reads to romd devices go through the ram_ptr found above,
74
+ target_ulong cs_base, uint32_t flags,
97
+ * but of course reads to I/O must go through MMIO.
75
+ uint32_t cflags)
98
+ */
76
+{
99
+ write_address |= TLB_MMIO;
77
+ tb_page_addr_t phys_pc;
100
+ if (!is_romd) {
78
+ struct tb_desc desc;
101
+ address = write_address;
79
+ uint32_t h;
102
+ }
80
+
103
}
81
+ desc.env = cpu->env_ptr;
104
82
+ desc.cs_base = cs_base;
105
- code_address = address;
83
+ desc.flags = flags;
106
- iotlb = memory_region_section_get_iotlb(cpu, section, vaddr_page,
84
+ desc.cflags = cflags;
107
- paddr_page, xlat, prot, &address);
85
+ desc.trace_vcpu_dstate = *cpu->trace_dstate;
108
wp_flags = cpu_watchpoint_address_matches(cpu, vaddr_page,
86
+ desc.pc = pc;
109
TARGET_PAGE_SIZE);
87
+ phys_pc = get_page_addr_code(desc.env, pc);
110
88
+ if (phys_pc == -1) {
111
@@ -XXX,XX +XXX,XX @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
89
+ return NULL;
112
/*
90
+ }
113
* At this point iotlb contains a physical section number in the lower
91
+ desc.phys_page1 = phys_pc & TARGET_PAGE_MASK;
114
* TARGET_PAGE_BITS, and either
92
+ h = tb_hash_func(phys_pc, pc, flags, cflags, *cpu->trace_dstate);
115
- * + the ram_addr_t of the page base of the target RAM (if NOTDIRTY or ROM)
93
+ return qht_lookup_custom(&tb_ctx.htable, &desc, h, tb_lookup_cmp);
116
- * + the offset within section->mr of the page base (otherwise)
94
+}
117
+ * + the ram_addr_t of the page base of the target RAM (RAM)
95
+
118
+ * + the offset within section->mr of the page base (I/O, ROMD)
96
/* Might cause an exception, so have a longjmp destination ready */
119
* We subtract the vaddr_page (which is page aligned and thus won't
97
static inline TranslationBlock *tb_lookup(CPUState *cpu, target_ulong pc,
120
* disturb the low bits) to give an offset which can be added to the
98
target_ulong cs_base,
121
* (non-page-aligned) vaddr of the eventual memory access to get
99
@@ -XXX,XX +XXX,XX @@ void cpu_exec_step_atomic(CPUState *cpu)
122
@@ -XXX,XX +XXX,XX @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
100
end_exclusive();
123
}
101
}
124
102
125
if (prot & PAGE_EXEC) {
103
-struct tb_desc {
126
- tn.addr_code = code_address;
104
- target_ulong pc;
127
+ tn.addr_code = address;
105
- target_ulong cs_base;
128
} else {
106
- CPUArchState *env;
129
tn.addr_code = -1;
107
- tb_page_addr_t phys_page1;
130
}
108
- uint32_t flags;
131
109
- uint32_t cflags;
132
tn.addr_write = -1;
110
- uint32_t trace_vcpu_dstate;
133
if (prot & PAGE_WRITE) {
111
-};
134
- tn.addr_write = address;
112
-
135
- if (memory_region_is_romd(section->mr)) {
113
-static bool tb_lookup_cmp(const void *p, const void *d)
136
- /* Use the MMIO path so that the device can switch states. */
114
-{
137
- tn.addr_write |= TLB_MMIO;
115
- const TranslationBlock *tb = p;
138
- } else if (memory_region_is_ram(section->mr)) {
116
- const struct tb_desc *desc = d;
139
- if (section->readonly) {
117
-
140
- tn.addr_write |= TLB_DISCARD_WRITE;
118
- if (tb->pc == desc->pc &&
141
- } else if (cpu_physical_memory_is_clean(
119
- tb->page_addr[0] == desc->phys_page1 &&
142
- memory_region_get_ram_addr(section->mr) + xlat)) {
120
- tb->cs_base == desc->cs_base &&
143
- tn.addr_write |= TLB_NOTDIRTY;
121
- tb->flags == desc->flags &&
122
- tb->trace_vcpu_dstate == desc->trace_vcpu_dstate &&
123
- tb_cflags(tb) == desc->cflags) {
124
- /* check next page if needed */
125
- if (tb->page_addr[1] == -1) {
126
- return true;
127
- } else {
128
- tb_page_addr_t phys_page2;
129
- target_ulong virt_page2;
130
-
131
- virt_page2 = (desc->pc & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
132
- phys_page2 = get_page_addr_code(desc->env, virt_page2);
133
- if (tb->page_addr[1] == phys_page2) {
134
- return true;
144
- }
135
- }
145
- }
136
- }
146
+ tn.addr_write = write_address;
137
- }
147
if (prot & PAGE_WRITE_INV) {
138
- return false;
148
tn.addr_write |= TLB_INVALID_MASK;
139
-}
149
}
140
-
150
diff --git a/exec.c b/exec.c
141
-TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,
151
index XXXXXXX..XXXXXXX 100644
142
- target_ulong cs_base, uint32_t flags,
152
--- a/exec.c
143
- uint32_t cflags)
153
+++ b/exec.c
144
-{
154
@@ -XXX,XX +XXX,XX @@ bool cpu_physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot *snap,
145
- tb_page_addr_t phys_pc;
155
146
- struct tb_desc desc;
156
/* Called from RCU critical section */
147
- uint32_t h;
157
hwaddr memory_region_section_get_iotlb(CPUState *cpu,
148
-
158
- MemoryRegionSection *section,
149
- desc.env = cpu->env_ptr;
159
- target_ulong vaddr,
150
- desc.cs_base = cs_base;
160
- hwaddr paddr, hwaddr xlat,
151
- desc.flags = flags;
161
- int prot,
152
- desc.cflags = cflags;
162
- target_ulong *address)
153
- desc.trace_vcpu_dstate = *cpu->trace_dstate;
163
+ MemoryRegionSection *section)
154
- desc.pc = pc;
155
- phys_pc = get_page_addr_code(desc.env, pc);
156
- if (phys_pc == -1) {
157
- return NULL;
158
- }
159
- desc.phys_page1 = phys_pc & TARGET_PAGE_MASK;
160
- h = tb_hash_func(phys_pc, pc, flags, cflags, *cpu->trace_dstate);
161
- return qht_lookup_custom(&tb_ctx.htable, &desc, h, tb_lookup_cmp);
162
-}
163
-
164
void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr)
164
{
165
{
165
- hwaddr iotlb;
166
if (TCG_TARGET_HAS_direct_jump) {
166
-
167
- if (memory_region_is_ram(section->mr)) {
168
- /* Normal RAM. */
169
- iotlb = memory_region_get_ram_addr(section->mr) + xlat;
170
- } else {
171
- AddressSpaceDispatch *d;
172
-
173
- d = flatview_to_dispatch(section->fv);
174
- iotlb = section - d->map.sections;
175
- iotlb += xlat;
176
- }
177
-
178
- return iotlb;
179
+ AddressSpaceDispatch *d = flatview_to_dispatch(section->fv);
180
+ return section - d->map.sections;
181
}
182
#endif /* defined(CONFIG_USER_ONLY) */
183
184
--
167
--
185
2.17.1
168
2.34.1
186
187
diff view generated by jsdifflib
1
Pages that we want to track for NOTDIRTY are RAM. We do not
1
The base qemu_ram_addr_from_host function is already in
2
really need to go through the I/O path to handle them.
2
softmmu/physmem.c; move the nofail version to be adjacent.
3
3
4
Acked-by: David Hildenbrand <david@redhat.com>
4
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
5
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
5
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
6
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
6
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
7
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
7
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
8
---
8
---
9
include/exec/cpu-common.h | 2 --
9
include/exec/cpu-common.h | 1 +
10
accel/tcg/cputlb.c | 26 +++++++++++++++++---
10
accel/tcg/cputlb.c | 12 ------------
11
exec.c | 50 ---------------------------------------
11
softmmu/physmem.c | 12 ++++++++++++
12
memory.c | 16 -------------
12
3 files changed, 13 insertions(+), 12 deletions(-)
13
4 files changed, 23 insertions(+), 71 deletions(-)
14
13
15
diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h
14
diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h
16
index XXXXXXX..XXXXXXX 100644
15
index XXXXXXX..XXXXXXX 100644
17
--- a/include/exec/cpu-common.h
16
--- a/include/exec/cpu-common.h
18
+++ b/include/exec/cpu-common.h
17
+++ b/include/exec/cpu-common.h
19
@@ -XXX,XX +XXX,XX @@ void qemu_flush_coalesced_mmio_buffer(void);
18
@@ -XXX,XX +XXX,XX @@ typedef uintptr_t ram_addr_t;
20
19
void qemu_ram_remap(ram_addr_t addr, ram_addr_t length);
21
void cpu_flush_icache_range(hwaddr start, hwaddr len);
20
/* This should not be used by devices. */
22
21
ram_addr_t qemu_ram_addr_from_host(void *ptr);
23
-extern struct MemoryRegion io_mem_notdirty;
22
+ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr);
24
-
23
RAMBlock *qemu_ram_block_by_name(const char *name);
25
typedef int (RAMBlockIterFunc)(RAMBlock *rb, void *opaque);
24
RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset,
26
25
ram_addr_t *offset);
27
int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque);
28
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
26
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
29
index XXXXXXX..XXXXXXX 100644
27
index XXXXXXX..XXXXXXX 100644
30
--- a/accel/tcg/cputlb.c
28
--- a/accel/tcg/cputlb.c
31
+++ b/accel/tcg/cputlb.c
29
+++ b/accel/tcg/cputlb.c
32
@@ -XXX,XX +XXX,XX @@ static uint64_t io_readx(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
30
@@ -XXX,XX +XXX,XX @@ void tlb_set_page(CPUState *cpu, target_ulong vaddr,
33
mr = section->mr;
31
prot, mmu_idx, size);
34
mr_offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
35
cpu->mem_io_pc = retaddr;
36
- if (mr != &io_mem_notdirty && !cpu->can_do_io) {
37
+ if (!cpu->can_do_io) {
38
cpu_io_recompile(cpu, retaddr);
39
}
40
41
@@ -XXX,XX +XXX,XX @@ static void io_writex(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
42
section = iotlb_to_section(cpu, iotlbentry->addr, iotlbentry->attrs);
43
mr = section->mr;
44
mr_offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
45
- if (mr != &io_mem_notdirty && !cpu->can_do_io) {
46
+ if (!cpu->can_do_io) {
47
cpu_io_recompile(cpu, retaddr);
48
}
49
cpu->mem_io_vaddr = addr;
50
@@ -XXX,XX +XXX,XX @@ store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
51
need_swap = size > 1 && (tlb_addr & TLB_BSWAP);
52
53
/* Handle I/O access. */
54
- if (likely(tlb_addr & (TLB_MMIO | TLB_NOTDIRTY))) {
55
+ if (tlb_addr & TLB_MMIO) {
56
io_writex(env, iotlbentry, mmu_idx, val, addr, retaddr,
57
op ^ (need_swap * MO_BSWAP));
58
return;
59
@@ -XXX,XX +XXX,XX @@ store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
60
61
haddr = (void *)((uintptr_t)addr + entry->addend);
62
63
+ /* Handle clean RAM pages. */
64
+ if (tlb_addr & TLB_NOTDIRTY) {
65
+ NotDirtyInfo ndi;
66
+
67
+ /* We require mem_io_pc in tb_invalidate_phys_page_range. */
68
+ env_cpu(env)->mem_io_pc = retaddr;
69
+
70
+ memory_notdirty_write_prepare(&ndi, env_cpu(env), addr,
71
+ addr + iotlbentry->addr, size);
72
+
73
+ if (unlikely(need_swap)) {
74
+ store_memop(haddr, val, op ^ MO_BSWAP);
75
+ } else {
76
+ store_memop(haddr, val, op);
77
+ }
78
+
79
+ memory_notdirty_write_complete(&ndi);
80
+ return;
81
+ }
82
+
83
/*
84
* Keep these two store_memop separate to ensure that the compiler
85
* is able to fold the entire function to a single instruction.
86
diff --git a/exec.c b/exec.c
87
index XXXXXXX..XXXXXXX 100644
88
--- a/exec.c
89
+++ b/exec.c
90
@@ -XXX,XX +XXX,XX @@ static MemoryRegion *system_io;
91
AddressSpace address_space_io;
92
AddressSpace address_space_memory;
93
94
-MemoryRegion io_mem_notdirty;
95
static MemoryRegion io_mem_unassigned;
96
#endif
97
98
@@ -XXX,XX +XXX,XX @@ typedef struct subpage_t {
99
} subpage_t;
100
101
#define PHYS_SECTION_UNASSIGNED 0
102
-#define PHYS_SECTION_NOTDIRTY 1
103
104
static void io_mem_init(void);
105
static void memory_map_init(void);
106
@@ -XXX,XX +XXX,XX @@ hwaddr memory_region_section_get_iotlb(CPUState *cpu,
107
if (memory_region_is_ram(section->mr)) {
108
/* Normal RAM. */
109
iotlb = memory_region_get_ram_addr(section->mr) + xlat;
110
- if (!section->readonly) {
111
- iotlb |= PHYS_SECTION_NOTDIRTY;
112
- }
113
} else {
114
AddressSpaceDispatch *d;
115
116
@@ -XXX,XX +XXX,XX @@ void memory_notdirty_write_complete(NotDirtyInfo *ndi)
117
}
118
}
32
}
119
33
120
-/* Called within RCU critical section. */
34
-static inline ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
121
-static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
122
- uint64_t val, unsigned size)
123
-{
35
-{
124
- NotDirtyInfo ndi;
36
- ram_addr_t ram_addr;
125
-
37
-
126
- memory_notdirty_write_prepare(&ndi, current_cpu, current_cpu->mem_io_vaddr,
38
- ram_addr = qemu_ram_addr_from_host(ptr);
127
- ram_addr, size);
39
- if (ram_addr == RAM_ADDR_INVALID) {
128
-
40
- error_report("Bad ram pointer %p", ptr);
129
- stn_p(qemu_map_ram_ptr(NULL, ram_addr), size, val);
41
- abort();
130
- memory_notdirty_write_complete(&ndi);
42
- }
43
- return ram_addr;
131
-}
44
-}
132
-
45
-
133
-static bool notdirty_mem_accepts(void *opaque, hwaddr addr,
46
/*
134
- unsigned size, bool is_write,
47
* Note: tlb_fill() can trigger a resize of the TLB. This means that all of the
135
- MemTxAttrs attrs)
48
* caller's prior references to the TLB table (e.g. CPUTLBEntry pointers) must
136
-{
49
diff --git a/softmmu/physmem.c b/softmmu/physmem.c
137
- return is_write;
50
index XXXXXXX..XXXXXXX 100644
138
-}
51
--- a/softmmu/physmem.c
139
-
52
+++ b/softmmu/physmem.c
140
-static const MemoryRegionOps notdirty_mem_ops = {
53
@@ -XXX,XX +XXX,XX @@ ram_addr_t qemu_ram_addr_from_host(void *ptr)
141
- .write = notdirty_mem_write,
54
return block->offset + offset;
142
- .valid.accepts = notdirty_mem_accepts,
143
- .endianness = DEVICE_NATIVE_ENDIAN,
144
- .valid = {
145
- .min_access_size = 1,
146
- .max_access_size = 8,
147
- .unaligned = false,
148
- },
149
- .impl = {
150
- .min_access_size = 1,
151
- .max_access_size = 8,
152
- .unaligned = false,
153
- },
154
-};
155
-
156
/* Generate a debug exception if a watchpoint has been hit. */
157
void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len,
158
MemTxAttrs attrs, int flags, uintptr_t ra)
159
@@ -XXX,XX +XXX,XX @@ static void io_mem_init(void)
160
{
161
memory_region_init_io(&io_mem_unassigned, NULL, &unassigned_mem_ops, NULL,
162
NULL, UINT64_MAX);
163
-
164
- /* io_mem_notdirty calls tb_invalidate_phys_page_fast,
165
- * which can be called without the iothread mutex.
166
- */
167
- memory_region_init_io(&io_mem_notdirty, NULL, &notdirty_mem_ops, NULL,
168
- NULL, UINT64_MAX);
169
- memory_region_clear_global_locking(&io_mem_notdirty);
170
}
55
}
171
56
172
AddressSpaceDispatch *address_space_dispatch_new(FlatView *fv)
57
+ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
173
@@ -XXX,XX +XXX,XX @@ AddressSpaceDispatch *address_space_dispatch_new(FlatView *fv)
58
+{
174
59
+ ram_addr_t ram_addr;
175
n = dummy_section(&d->map, fv, &io_mem_unassigned);
60
+
176
assert(n == PHYS_SECTION_UNASSIGNED);
61
+ ram_addr = qemu_ram_addr_from_host(ptr);
177
- n = dummy_section(&d->map, fv, &io_mem_notdirty);
62
+ if (ram_addr == RAM_ADDR_INVALID) {
178
- assert(n == PHYS_SECTION_NOTDIRTY);
63
+ error_report("Bad ram pointer %p", ptr);
179
64
+ abort();
180
d->phys_map = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 };
65
+ }
181
66
+ return ram_addr;
182
diff --git a/memory.c b/memory.c
67
+}
183
index XXXXXXX..XXXXXXX 100644
68
+
184
--- a/memory.c
69
static MemTxResult flatview_read(FlatView *fv, hwaddr addr,
185
+++ b/memory.c
70
MemTxAttrs attrs, void *buf, hwaddr len);
186
@@ -XXX,XX +XXX,XX @@ static MemTxResult memory_region_read_accessor(MemoryRegion *mr,
71
static MemTxResult flatview_write(FlatView *fv, hwaddr addr, MemTxAttrs attrs,
187
tmp = mr->ops->read(mr->opaque, addr, size);
188
if (mr->subpage) {
189
trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
190
- } else if (mr == &io_mem_notdirty) {
191
- /* Accesses to code which has previously been translated into a TB show
192
- * up in the MMIO path, as accesses to the io_mem_notdirty
193
- * MemoryRegion. */
194
} else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
195
hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
196
trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
197
@@ -XXX,XX +XXX,XX @@ static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr,
198
r = mr->ops->read_with_attrs(mr->opaque, addr, &tmp, size, attrs);
199
if (mr->subpage) {
200
trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
201
- } else if (mr == &io_mem_notdirty) {
202
- /* Accesses to code which has previously been translated into a TB show
203
- * up in the MMIO path, as accesses to the io_mem_notdirty
204
- * MemoryRegion. */
205
} else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
206
hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
207
trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
208
@@ -XXX,XX +XXX,XX @@ static MemTxResult memory_region_write_accessor(MemoryRegion *mr,
209
210
if (mr->subpage) {
211
trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
212
- } else if (mr == &io_mem_notdirty) {
213
- /* Accesses to code which has previously been translated into a TB show
214
- * up in the MMIO path, as accesses to the io_mem_notdirty
215
- * MemoryRegion. */
216
} else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
217
hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
218
trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
219
@@ -XXX,XX +XXX,XX @@ static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr,
220
221
if (mr->subpage) {
222
trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
223
- } else if (mr == &io_mem_notdirty) {
224
- /* Accesses to code which has previously been translated into a TB show
225
- * up in the MMIO path, as accesses to the io_mem_notdirty
226
- * MemoryRegion. */
227
} else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
228
hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
229
trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
230
--
72
--
231
2.17.1
73
2.34.1
232
233
diff view generated by jsdifflib
1
Handle bswap on ram directly in load/store_helper. This fixes a
1
Simplify the implementation of get_page_addr_code_hostp
2
bug with the previous implementation in that one cannot use the
2
by reusing the existing probe_access infrastructure.
3
I/O path for RAM.
4
3
5
Fixes: a26fc6f5152b47f1
4
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
6
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
5
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
7
Reviewed-by: David Hildenbrand <david@redhat.com>
8
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
6
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
9
---
7
---
10
include/exec/cpu-all.h | 4 ++-
8
accel/tcg/cputlb.c | 76 ++++++++++++++++------------------------------
11
accel/tcg/cputlb.c | 72 +++++++++++++++++++++++++-----------------
9
1 file changed, 26 insertions(+), 50 deletions(-)
12
2 files changed, 46 insertions(+), 30 deletions(-)
13
10
14
diff --git a/include/exec/cpu-all.h b/include/exec/cpu-all.h
15
index XXXXXXX..XXXXXXX 100644
16
--- a/include/exec/cpu-all.h
17
+++ b/include/exec/cpu-all.h
18
@@ -XXX,XX +XXX,XX @@ CPUArchState *cpu_copy(CPUArchState *env);
19
#define TLB_MMIO (1 << (TARGET_PAGE_BITS_MIN - 3))
20
/* Set if TLB entry contains a watchpoint. */
21
#define TLB_WATCHPOINT (1 << (TARGET_PAGE_BITS_MIN - 4))
22
+/* Set if TLB entry requires byte swap. */
23
+#define TLB_BSWAP (1 << (TARGET_PAGE_BITS_MIN - 5))
24
25
/* Use this mask to check interception with an alignment mask
26
* in a TCG backend.
27
*/
28
#define TLB_FLAGS_MASK \
29
- (TLB_INVALID_MASK | TLB_NOTDIRTY | TLB_MMIO | TLB_WATCHPOINT)
30
+ (TLB_INVALID_MASK | TLB_NOTDIRTY | TLB_MMIO | TLB_WATCHPOINT | TLB_BSWAP)
31
32
/**
33
* tlb_hit_page: return true if page aligned @addr is a hit against the
34
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
11
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
35
index XXXXXXX..XXXXXXX 100644
12
index XXXXXXX..XXXXXXX 100644
36
--- a/accel/tcg/cputlb.c
13
--- a/accel/tcg/cputlb.c
37
+++ b/accel/tcg/cputlb.c
14
+++ b/accel/tcg/cputlb.c
38
@@ -XXX,XX +XXX,XX @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
15
@@ -XXX,XX +XXX,XX @@ static bool victim_tlb_hit(CPUArchState *env, size_t mmu_idx, size_t index,
39
address |= TLB_INVALID_MASK;
16
victim_tlb_hit(env, mmu_idx, index, offsetof(CPUTLBEntry, TY), \
40
}
17
(ADDR) & TARGET_PAGE_MASK)
41
if (attrs.byte_swap) {
18
42
- /* Force the access through the I/O slow path. */
19
-/*
43
- address |= TLB_MMIO;
20
- * Return a ram_addr_t for the virtual address for execution.
44
+ address |= TLB_BSWAP;
21
- *
45
}
22
- * Return -1 if we can't translate and execute from an entire page
46
if (!memory_region_is_ram(section->mr) &&
23
- * of RAM. This will force us to execute by loading and translating
47
!memory_region_is_romd(section->mr)) {
24
- * one insn at a time, without caching.
48
@@ -XXX,XX +XXX,XX @@ static uint64_t io_readx(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
25
- *
49
bool locked = false;
26
- * NOTE: This function will trigger an exception if the page is
50
MemTxResult r;
27
- * not executable.
51
28
- */
52
- if (iotlbentry->attrs.byte_swap) {
29
-tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr,
53
- op ^= MO_BSWAP;
30
- void **hostp)
31
-{
32
- uintptr_t mmu_idx = cpu_mmu_index(env, true);
33
- uintptr_t index = tlb_index(env, mmu_idx, addr);
34
- CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
35
- void *p;
36
-
37
- if (unlikely(!tlb_hit(entry->addr_code, addr))) {
38
- if (!VICTIM_TLB_HIT(addr_code, addr)) {
39
- tlb_fill(env_cpu(env), addr, 0, MMU_INST_FETCH, mmu_idx, 0);
40
- index = tlb_index(env, mmu_idx, addr);
41
- entry = tlb_entry(env, mmu_idx, addr);
42
-
43
- if (unlikely(entry->addr_code & TLB_INVALID_MASK)) {
44
- /*
45
- * The MMU protection covers a smaller range than a target
46
- * page, so we must redo the MMU check for every insn.
47
- */
48
- return -1;
49
- }
50
- }
51
- assert(tlb_hit(entry->addr_code, addr));
54
- }
52
- }
55
-
53
-
56
section = iotlb_to_section(cpu, iotlbentry->addr, iotlbentry->attrs);
54
- if (unlikely(entry->addr_code & TLB_MMIO)) {
57
mr = section->mr;
55
- /* The region is not backed by RAM. */
58
mr_offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
56
- if (hostp) {
59
@@ -XXX,XX +XXX,XX @@ static void io_writex(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
57
- *hostp = NULL;
60
bool locked = false;
58
- }
61
MemTxResult r;
59
- return -1;
62
63
- if (iotlbentry->attrs.byte_swap) {
64
- op ^= MO_BSWAP;
65
- }
60
- }
66
-
61
-
67
section = iotlb_to_section(cpu, iotlbentry->addr, iotlbentry->attrs);
62
- p = (void *)((uintptr_t)addr + entry->addend);
68
mr = section->mr;
63
- if (hostp) {
69
mr_offset = (iotlbentry->addr & TARGET_PAGE_MASK) + addr;
64
- *hostp = p;
70
@@ -XXX,XX +XXX,XX @@ void *probe_access(CPUArchState *env, target_ulong addr, int size,
65
- }
71
wp_access, retaddr);
66
- return qemu_ram_addr_from_host_nofail(p);
72
}
67
-}
73
74
- if (tlb_addr & (TLB_NOTDIRTY | TLB_MMIO)) {
75
- /* I/O access */
76
+ /* Reject I/O access, or other required slow-path. */
77
+ if (tlb_addr & (TLB_NOTDIRTY | TLB_MMIO | TLB_BSWAP)) {
78
return NULL;
79
}
80
81
@@ -XXX,XX +XXX,XX @@ load_helper(CPUArchState *env, target_ulong addr, TCGMemOpIdx oi,
82
/* Handle anything that isn't just a straight memory access. */
83
if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
84
CPUIOTLBEntry *iotlbentry;
85
+ bool need_swap;
86
87
/* For anything that is unaligned, recurse through full_load. */
88
if ((addr & (size - 1)) != 0) {
89
@@ -XXX,XX +XXX,XX @@ load_helper(CPUArchState *env, target_ulong addr, TCGMemOpIdx oi,
90
/* On watchpoint hit, this will longjmp out. */
91
cpu_check_watchpoint(env_cpu(env), addr, size,
92
iotlbentry->attrs, BP_MEM_READ, retaddr);
93
-
68
-
94
- /* The backing page may or may not require I/O. */
69
static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size,
95
- tlb_addr &= ~TLB_WATCHPOINT;
70
CPUIOTLBEntry *iotlbentry, uintptr_t retaddr)
96
- if ((tlb_addr & ~TARGET_PAGE_MASK) == 0) {
71
{
97
- goto do_aligned_access;
72
@@ -XXX,XX +XXX,XX @@ void *tlb_vaddr_to_host(CPUArchState *env, abi_ptr addr,
98
- }
73
return flags ? NULL : host;
99
}
74
}
100
75
101
+ need_swap = size > 1 && (tlb_addr & TLB_BSWAP);
76
+/*
77
+ * Return a ram_addr_t for the virtual address for execution.
78
+ *
79
+ * Return -1 if we can't translate and execute from an entire page
80
+ * of RAM. This will force us to execute by loading and translating
81
+ * one insn at a time, without caching.
82
+ *
83
+ * NOTE: This function will trigger an exception if the page is
84
+ * not executable.
85
+ */
86
+tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr,
87
+ void **hostp)
88
+{
89
+ void *p;
102
+
90
+
103
/* Handle I/O access. */
91
+ (void)probe_access_internal(env, addr, 1, MMU_INST_FETCH,
104
- return io_readx(env, iotlbentry, mmu_idx, addr,
92
+ cpu_mmu_index(env, true), false, &p, 0);
105
- retaddr, access_type, op);
93
+ if (p == NULL) {
106
+ if (likely(tlb_addr & TLB_MMIO)) {
94
+ return -1;
107
+ return io_readx(env, iotlbentry, mmu_idx, addr, retaddr,
95
+ }
108
+ access_type, op ^ (need_swap * MO_BSWAP));
96
+ if (hostp) {
109
+ }
97
+ *hostp = p;
98
+ }
99
+ return qemu_ram_addr_from_host_nofail(p);
100
+}
110
+
101
+
111
+ haddr = (void *)((uintptr_t)addr + entry->addend);
102
#ifdef CONFIG_PLUGIN
112
+
103
/*
113
+ /*
104
* Perform a TLB lookup and populate the qemu_plugin_hwaddr structure.
114
+ * Keep these two load_memop separate to ensure that the compiler
115
+ * is able to fold the entire function to a single instruction.
116
+ * There is a build-time assert inside to remind you of this. ;-)
117
+ */
118
+ if (unlikely(need_swap)) {
119
+ return load_memop(haddr, op ^ MO_BSWAP);
120
+ }
121
+ return load_memop(haddr, op);
122
}
123
124
/* Handle slow unaligned access (it spans two pages or IO). */
125
@@ -XXX,XX +XXX,XX @@ load_helper(CPUArchState *env, target_ulong addr, TCGMemOpIdx oi,
126
return res & MAKE_64BIT_MASK(0, size * 8);
127
}
128
129
- do_aligned_access:
130
haddr = (void *)((uintptr_t)addr + entry->addend);
131
return load_memop(haddr, op);
132
}
133
@@ -XXX,XX +XXX,XX @@ store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
134
/* Handle anything that isn't just a straight memory access. */
135
if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
136
CPUIOTLBEntry *iotlbentry;
137
+ bool need_swap;
138
139
/* For anything that is unaligned, recurse through byte stores. */
140
if ((addr & (size - 1)) != 0) {
141
@@ -XXX,XX +XXX,XX @@ store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
142
/* On watchpoint hit, this will longjmp out. */
143
cpu_check_watchpoint(env_cpu(env), addr, size,
144
iotlbentry->attrs, BP_MEM_WRITE, retaddr);
145
-
146
- /* The backing page may or may not require I/O. */
147
- tlb_addr &= ~TLB_WATCHPOINT;
148
- if ((tlb_addr & ~TARGET_PAGE_MASK) == 0) {
149
- goto do_aligned_access;
150
- }
151
}
152
153
+ need_swap = size > 1 && (tlb_addr & TLB_BSWAP);
154
+
155
/* Handle I/O access. */
156
- io_writex(env, iotlbentry, mmu_idx, val, addr, retaddr, op);
157
+ if (likely(tlb_addr & (TLB_MMIO | TLB_NOTDIRTY))) {
158
+ io_writex(env, iotlbentry, mmu_idx, val, addr, retaddr,
159
+ op ^ (need_swap * MO_BSWAP));
160
+ return;
161
+ }
162
+
163
+ haddr = (void *)((uintptr_t)addr + entry->addend);
164
+
165
+ /*
166
+ * Keep these two store_memop separate to ensure that the compiler
167
+ * is able to fold the entire function to a single instruction.
168
+ * There is a build-time assert inside to remind you of this. ;-)
169
+ */
170
+ if (unlikely(need_swap)) {
171
+ store_memop(haddr, val, op ^ MO_BSWAP);
172
+ } else {
173
+ store_memop(haddr, val, op);
174
+ }
175
return;
176
}
177
178
@@ -XXX,XX +XXX,XX @@ store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
179
return;
180
}
181
182
- do_aligned_access:
183
haddr = (void *)((uintptr_t)addr + entry->addend);
184
store_memop(haddr, val, op);
185
}
186
--
105
--
187
2.17.1
106
2.34.1
188
189
diff view generated by jsdifflib
New patch
1
It was non-obvious to me why we can raise an exception in
2
the middle of a comparison function, but it works.
3
While nearby, use TARGET_PAGE_ALIGN instead of open-coding.
1
4
5
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
6
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
7
---
8
accel/tcg/cpu-exec.c | 11 ++++++++++-
9
1 file changed, 10 insertions(+), 1 deletion(-)
10
11
diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c
12
index XXXXXXX..XXXXXXX 100644
13
--- a/accel/tcg/cpu-exec.c
14
+++ b/accel/tcg/cpu-exec.c
15
@@ -XXX,XX +XXX,XX @@ static bool tb_lookup_cmp(const void *p, const void *d)
16
tb_page_addr_t phys_page2;
17
target_ulong virt_page2;
18
19
- virt_page2 = (desc->pc & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
20
+ /*
21
+ * We know that the first page matched, and an otherwise valid TB
22
+ * encountered an incomplete instruction at the end of that page,
23
+ * therefore we know that generating a new TB from the current PC
24
+ * must also require reading from the next page -- even if the
25
+ * second pages do not match, and therefore the resulting insn
26
+ * is different for the new TB. Therefore any exception raised
27
+ * here by the faulting lookup is not premature.
28
+ */
29
+ virt_page2 = TARGET_PAGE_ALIGN(desc->pc);
30
phys_page2 = get_page_addr_code(desc->env, virt_page2);
31
if (tb->page_addr[1] == phys_page2) {
32
return true;
33
--
34
2.34.1
diff view generated by jsdifflib
New patch
1
The only user can easily use translator_lduw and
2
adjust the type to signed during the return.
1
3
4
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
5
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
6
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
7
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
8
---
9
include/exec/translator.h | 1 -
10
target/i386/tcg/translate.c | 2 +-
11
2 files changed, 1 insertion(+), 2 deletions(-)
12
13
diff --git a/include/exec/translator.h b/include/exec/translator.h
14
index XXXXXXX..XXXXXXX 100644
15
--- a/include/exec/translator.h
16
+++ b/include/exec/translator.h
17
@@ -XXX,XX +XXX,XX @@ bool translator_use_goto_tb(DisasContextBase *db, target_ulong dest);
18
19
#define FOR_EACH_TRANSLATOR_LD(F) \
20
F(translator_ldub, uint8_t, cpu_ldub_code, /* no swap */) \
21
- F(translator_ldsw, int16_t, cpu_ldsw_code, bswap16) \
22
F(translator_lduw, uint16_t, cpu_lduw_code, bswap16) \
23
F(translator_ldl, uint32_t, cpu_ldl_code, bswap32) \
24
F(translator_ldq, uint64_t, cpu_ldq_code, bswap64)
25
diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c
26
index XXXXXXX..XXXXXXX 100644
27
--- a/target/i386/tcg/translate.c
28
+++ b/target/i386/tcg/translate.c
29
@@ -XXX,XX +XXX,XX @@ static inline uint8_t x86_ldub_code(CPUX86State *env, DisasContext *s)
30
31
static inline int16_t x86_ldsw_code(CPUX86State *env, DisasContext *s)
32
{
33
- return translator_ldsw(env, &s->base, advance_pc(env, s, 2));
34
+ return translator_lduw(env, &s->base, advance_pc(env, s, 2));
35
}
36
37
static inline uint16_t x86_lduw_code(CPUX86State *env, DisasContext *s)
38
--
39
2.34.1
diff view generated by jsdifflib
1
Fixes the previous TLB_WATCHPOINT patches because we are currently
1
Pass these along to translator_loop -- pc may be used instead
2
failing to set cpu->mem_io_pc with the call to cpu_check_watchpoint.
2
of tb->pc, and host_pc is currently unused. Adjust all targets
3
Pass down the retaddr directly because it's readily available.
3
at one time.
4
4
5
Fixes: 50b107c5d61
5
Acked-by: Alistair Francis <alistair.francis@wdc.com>
6
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
6
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
7
Reviewed-by: David Hildenbrand <david@redhat.com>
7
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
8
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
8
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
9
---
9
---
10
accel/tcg/translate-all.h | 2 +-
10
include/exec/exec-all.h | 1 -
11
accel/tcg/translate-all.c | 6 +++---
11
include/exec/translator.h | 24 ++++++++++++++++++++----
12
exec.c | 2 +-
12
accel/tcg/translate-all.c | 6 ++++--
13
3 files changed, 5 insertions(+), 5 deletions(-)
13
accel/tcg/translator.c | 9 +++++----
14
target/alpha/translate.c | 5 +++--
15
target/arm/translate.c | 5 +++--
16
target/avr/translate.c | 5 +++--
17
target/cris/translate.c | 5 +++--
18
target/hexagon/translate.c | 6 ++++--
19
target/hppa/translate.c | 5 +++--
20
target/i386/tcg/translate.c | 5 +++--
21
target/loongarch/translate.c | 6 ++++--
22
target/m68k/translate.c | 5 +++--
23
target/microblaze/translate.c | 5 +++--
24
target/mips/tcg/translate.c | 5 +++--
25
target/nios2/translate.c | 5 +++--
26
target/openrisc/translate.c | 6 ++++--
27
target/ppc/translate.c | 5 +++--
28
target/riscv/translate.c | 5 +++--
29
target/rx/translate.c | 5 +++--
30
target/s390x/tcg/translate.c | 5 +++--
31
target/sh4/translate.c | 5 +++--
32
target/sparc/translate.c | 5 +++--
33
target/tricore/translate.c | 6 ++++--
34
target/xtensa/translate.c | 6 ++++--
35
25 files changed, 97 insertions(+), 53 deletions(-)
14
36
15
diff --git a/accel/tcg/translate-all.h b/accel/tcg/translate-all.h
37
diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h
16
index XXXXXXX..XXXXXXX 100644
38
index XXXXXXX..XXXXXXX 100644
17
--- a/accel/tcg/translate-all.h
39
--- a/include/exec/exec-all.h
18
+++ b/accel/tcg/translate-all.h
40
+++ b/include/exec/exec-all.h
19
@@ -XXX,XX +XXX,XX @@ void tb_invalidate_phys_page_fast(struct page_collection *pages,
41
@@ -XXX,XX +XXX,XX @@ typedef ram_addr_t tb_page_addr_t;
20
tb_page_addr_t start, int len,
42
#define TB_PAGE_ADDR_FMT RAM_ADDR_FMT
21
uintptr_t retaddr);
43
#endif
22
void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end);
44
23
-void tb_check_watchpoint(CPUState *cpu);
45
-void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns);
24
+void tb_check_watchpoint(CPUState *cpu, uintptr_t retaddr);
46
void restore_state_to_opc(CPUArchState *env, TranslationBlock *tb,
25
47
target_ulong *data);
26
#ifdef CONFIG_USER_ONLY
48
27
int page_unprotect(target_ulong address, uintptr_t pc);
49
diff --git a/include/exec/translator.h b/include/exec/translator.h
50
index XXXXXXX..XXXXXXX 100644
51
--- a/include/exec/translator.h
52
+++ b/include/exec/translator.h
53
@@ -XXX,XX +XXX,XX @@
54
#include "exec/translate-all.h"
55
#include "tcg/tcg.h"
56
57
+/**
58
+ * gen_intermediate_code
59
+ * @cpu: cpu context
60
+ * @tb: translation block
61
+ * @max_insns: max number of instructions to translate
62
+ * @pc: guest virtual program counter address
63
+ * @host_pc: host physical program counter address
64
+ *
65
+ * This function must be provided by the target, which should create
66
+ * the target-specific DisasContext, and then invoke translator_loop.
67
+ */
68
+void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns,
69
+ target_ulong pc, void *host_pc);
70
71
/**
72
* DisasJumpType:
73
@@ -XXX,XX +XXX,XX @@ typedef struct TranslatorOps {
74
75
/**
76
* translator_loop:
77
- * @ops: Target-specific operations.
78
- * @db: Disassembly context.
79
* @cpu: Target vCPU.
80
* @tb: Translation block.
81
* @max_insns: Maximum number of insns to translate.
82
+ * @pc: guest virtual program counter address
83
+ * @host_pc: host physical program counter address
84
+ * @ops: Target-specific operations.
85
+ * @db: Disassembly context.
86
*
87
* Generic translator loop.
88
*
89
@@ -XXX,XX +XXX,XX @@ typedef struct TranslatorOps {
90
* - When single-stepping is enabled (system-wide or on the current vCPU).
91
* - When too many instructions have been translated.
92
*/
93
-void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
94
- CPUState *cpu, TranslationBlock *tb, int max_insns);
95
+void translator_loop(CPUState *cpu, TranslationBlock *tb, int max_insns,
96
+ target_ulong pc, void *host_pc,
97
+ const TranslatorOps *ops, DisasContextBase *db);
98
99
void translator_loop_temp_check(DisasContextBase *db);
100
28
diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c
101
diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c
29
index XXXXXXX..XXXXXXX 100644
102
index XXXXXXX..XXXXXXX 100644
30
--- a/accel/tcg/translate-all.c
103
--- a/accel/tcg/translate-all.c
31
+++ b/accel/tcg/translate-all.c
104
+++ b/accel/tcg/translate-all.c
32
@@ -XXX,XX +XXX,XX @@ static bool tb_invalidate_phys_page(tb_page_addr_t addr, uintptr_t pc)
105
@@ -XXX,XX +XXX,XX @@
106
107
#include "exec/cputlb.h"
108
#include "exec/translate-all.h"
109
+#include "exec/translator.h"
110
#include "qemu/bitmap.h"
111
#include "qemu/qemu-print.h"
112
#include "qemu/timer.h"
113
@@ -XXX,XX +XXX,XX @@ TranslationBlock *tb_gen_code(CPUState *cpu,
114
TCGProfile *prof = &tcg_ctx->prof;
115
int64_t ti;
33
#endif
116
#endif
34
117
+ void *host_pc;
35
/* user-mode: call with mmap_lock held */
36
-void tb_check_watchpoint(CPUState *cpu)
37
+void tb_check_watchpoint(CPUState *cpu, uintptr_t retaddr)
38
{
39
TranslationBlock *tb;
40
118
41
assert_memory_lock();
119
assert_memory_lock();
42
120
qemu_thread_jit_write();
43
- tb = tcg_tb_lookup(cpu->mem_io_pc);
121
44
+ tb = tcg_tb_lookup(retaddr);
122
- phys_pc = get_page_addr_code(env, pc);
45
if (tb) {
123
+ phys_pc = get_page_addr_code_hostp(env, pc, &host_pc);
46
/* We can use retranslation to find the PC. */
124
47
- cpu_restore_state_from_tb(cpu, tb, cpu->mem_io_pc, true);
125
if (phys_pc == -1) {
48
+ cpu_restore_state_from_tb(cpu, tb, retaddr, true);
126
/* Generate a one-shot TB with 1 insn in it */
49
tb_phys_invalidate(tb, -1);
127
@@ -XXX,XX +XXX,XX @@ TranslationBlock *tb_gen_code(CPUState *cpu,
50
} else {
128
tcg_func_start(tcg_ctx);
51
/* The exception probably happened in a helper. The CPU state should
129
52
diff --git a/exec.c b/exec.c
130
tcg_ctx->cpu = env_cpu(env);
53
index XXXXXXX..XXXXXXX 100644
131
- gen_intermediate_code(cpu, tb, max_insns);
54
--- a/exec.c
132
+ gen_intermediate_code(cpu, tb, max_insns, pc, host_pc);
55
+++ b/exec.c
133
assert(tb->size != 0);
56
@@ -XXX,XX +XXX,XX @@ void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len,
134
tcg_ctx->cpu = NULL;
57
cpu->watchpoint_hit = wp;
135
max_insns = tb->icount;
58
136
diff --git a/accel/tcg/translator.c b/accel/tcg/translator.c
59
mmap_lock();
137
index XXXXXXX..XXXXXXX 100644
60
- tb_check_watchpoint(cpu);
138
--- a/accel/tcg/translator.c
61
+ tb_check_watchpoint(cpu, ra);
139
+++ b/accel/tcg/translator.c
62
if (wp->flags & BP_STOP_BEFORE_ACCESS) {
140
@@ -XXX,XX +XXX,XX @@ static inline void translator_page_protect(DisasContextBase *dcbase,
63
cpu->exception_index = EXCP_DEBUG;
141
#endif
64
mmap_unlock();
142
}
143
144
-void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
145
- CPUState *cpu, TranslationBlock *tb, int max_insns)
146
+void translator_loop(CPUState *cpu, TranslationBlock *tb, int max_insns,
147
+ target_ulong pc, void *host_pc,
148
+ const TranslatorOps *ops, DisasContextBase *db)
149
{
150
uint32_t cflags = tb_cflags(tb);
151
bool plugin_enabled;
152
153
/* Initialize DisasContext */
154
db->tb = tb;
155
- db->pc_first = tb->pc;
156
- db->pc_next = db->pc_first;
157
+ db->pc_first = pc;
158
+ db->pc_next = pc;
159
db->is_jmp = DISAS_NEXT;
160
db->num_insns = 0;
161
db->max_insns = max_insns;
162
diff --git a/target/alpha/translate.c b/target/alpha/translate.c
163
index XXXXXXX..XXXXXXX 100644
164
--- a/target/alpha/translate.c
165
+++ b/target/alpha/translate.c
166
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps alpha_tr_ops = {
167
.disas_log = alpha_tr_disas_log,
168
};
169
170
-void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns)
171
+void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns,
172
+ target_ulong pc, void *host_pc)
173
{
174
DisasContext dc;
175
- translator_loop(&alpha_tr_ops, &dc.base, cpu, tb, max_insns);
176
+ translator_loop(cpu, tb, max_insns, pc, host_pc, &alpha_tr_ops, &dc.base);
177
}
178
179
void restore_state_to_opc(CPUAlphaState *env, TranslationBlock *tb,
180
diff --git a/target/arm/translate.c b/target/arm/translate.c
181
index XXXXXXX..XXXXXXX 100644
182
--- a/target/arm/translate.c
183
+++ b/target/arm/translate.c
184
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps thumb_translator_ops = {
185
};
186
187
/* generate intermediate code for basic block 'tb'. */
188
-void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns)
189
+void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns,
190
+ target_ulong pc, void *host_pc)
191
{
192
DisasContext dc = { };
193
const TranslatorOps *ops = &arm_translator_ops;
194
@@ -XXX,XX +XXX,XX @@ void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns)
195
}
196
#endif
197
198
- translator_loop(ops, &dc.base, cpu, tb, max_insns);
199
+ translator_loop(cpu, tb, max_insns, pc, host_pc, ops, &dc.base);
200
}
201
202
void restore_state_to_opc(CPUARMState *env, TranslationBlock *tb,
203
diff --git a/target/avr/translate.c b/target/avr/translate.c
204
index XXXXXXX..XXXXXXX 100644
205
--- a/target/avr/translate.c
206
+++ b/target/avr/translate.c
207
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps avr_tr_ops = {
208
.disas_log = avr_tr_disas_log,
209
};
210
211
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
212
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
213
+ target_ulong pc, void *host_pc)
214
{
215
DisasContext dc = { };
216
- translator_loop(&avr_tr_ops, &dc.base, cs, tb, max_insns);
217
+ translator_loop(cs, tb, max_insns, pc, host_pc, &avr_tr_ops, &dc.base);
218
}
219
220
void restore_state_to_opc(CPUAVRState *env, TranslationBlock *tb,
221
diff --git a/target/cris/translate.c b/target/cris/translate.c
222
index XXXXXXX..XXXXXXX 100644
223
--- a/target/cris/translate.c
224
+++ b/target/cris/translate.c
225
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps cris_tr_ops = {
226
.disas_log = cris_tr_disas_log,
227
};
228
229
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
230
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
231
+ target_ulong pc, void *host_pc)
232
{
233
DisasContext dc;
234
- translator_loop(&cris_tr_ops, &dc.base, cs, tb, max_insns);
235
+ translator_loop(cs, tb, max_insns, pc, host_pc, &cris_tr_ops, &dc.base);
236
}
237
238
void cris_cpu_dump_state(CPUState *cs, FILE *f, int flags)
239
diff --git a/target/hexagon/translate.c b/target/hexagon/translate.c
240
index XXXXXXX..XXXXXXX 100644
241
--- a/target/hexagon/translate.c
242
+++ b/target/hexagon/translate.c
243
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps hexagon_tr_ops = {
244
.disas_log = hexagon_tr_disas_log,
245
};
246
247
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
248
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
249
+ target_ulong pc, void *host_pc)
250
{
251
DisasContext ctx;
252
253
- translator_loop(&hexagon_tr_ops, &ctx.base, cs, tb, max_insns);
254
+ translator_loop(cs, tb, max_insns, pc, host_pc,
255
+ &hexagon_tr_ops, &ctx.base);
256
}
257
258
#define NAME_LEN 64
259
diff --git a/target/hppa/translate.c b/target/hppa/translate.c
260
index XXXXXXX..XXXXXXX 100644
261
--- a/target/hppa/translate.c
262
+++ b/target/hppa/translate.c
263
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps hppa_tr_ops = {
264
.disas_log = hppa_tr_disas_log,
265
};
266
267
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
268
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
269
+ target_ulong pc, void *host_pc)
270
{
271
DisasContext ctx;
272
- translator_loop(&hppa_tr_ops, &ctx.base, cs, tb, max_insns);
273
+ translator_loop(cs, tb, max_insns, pc, host_pc, &hppa_tr_ops, &ctx.base);
274
}
275
276
void restore_state_to_opc(CPUHPPAState *env, TranslationBlock *tb,
277
diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c
278
index XXXXXXX..XXXXXXX 100644
279
--- a/target/i386/tcg/translate.c
280
+++ b/target/i386/tcg/translate.c
281
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps i386_tr_ops = {
282
};
283
284
/* generate intermediate code for basic block 'tb'. */
285
-void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns)
286
+void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns,
287
+ target_ulong pc, void *host_pc)
288
{
289
DisasContext dc;
290
291
- translator_loop(&i386_tr_ops, &dc.base, cpu, tb, max_insns);
292
+ translator_loop(cpu, tb, max_insns, pc, host_pc, &i386_tr_ops, &dc.base);
293
}
294
295
void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb,
296
diff --git a/target/loongarch/translate.c b/target/loongarch/translate.c
297
index XXXXXXX..XXXXXXX 100644
298
--- a/target/loongarch/translate.c
299
+++ b/target/loongarch/translate.c
300
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps loongarch_tr_ops = {
301
.disas_log = loongarch_tr_disas_log,
302
};
303
304
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
305
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
306
+ target_ulong pc, void *host_pc)
307
{
308
DisasContext ctx;
309
310
- translator_loop(&loongarch_tr_ops, &ctx.base, cs, tb, max_insns);
311
+ translator_loop(cs, tb, max_insns, pc, host_pc,
312
+ &loongarch_tr_ops, &ctx.base);
313
}
314
315
void loongarch_translate_init(void)
316
diff --git a/target/m68k/translate.c b/target/m68k/translate.c
317
index XXXXXXX..XXXXXXX 100644
318
--- a/target/m68k/translate.c
319
+++ b/target/m68k/translate.c
320
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps m68k_tr_ops = {
321
.disas_log = m68k_tr_disas_log,
322
};
323
324
-void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns)
325
+void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns,
326
+ target_ulong pc, void *host_pc)
327
{
328
DisasContext dc;
329
- translator_loop(&m68k_tr_ops, &dc.base, cpu, tb, max_insns);
330
+ translator_loop(cpu, tb, max_insns, pc, host_pc, &m68k_tr_ops, &dc.base);
331
}
332
333
static double floatx80_to_double(CPUM68KState *env, uint16_t high, uint64_t low)
334
diff --git a/target/microblaze/translate.c b/target/microblaze/translate.c
335
index XXXXXXX..XXXXXXX 100644
336
--- a/target/microblaze/translate.c
337
+++ b/target/microblaze/translate.c
338
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps mb_tr_ops = {
339
.disas_log = mb_tr_disas_log,
340
};
341
342
-void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns)
343
+void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns,
344
+ target_ulong pc, void *host_pc)
345
{
346
DisasContext dc;
347
- translator_loop(&mb_tr_ops, &dc.base, cpu, tb, max_insns);
348
+ translator_loop(cpu, tb, max_insns, pc, host_pc, &mb_tr_ops, &dc.base);
349
}
350
351
void mb_cpu_dump_state(CPUState *cs, FILE *f, int flags)
352
diff --git a/target/mips/tcg/translate.c b/target/mips/tcg/translate.c
353
index XXXXXXX..XXXXXXX 100644
354
--- a/target/mips/tcg/translate.c
355
+++ b/target/mips/tcg/translate.c
356
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps mips_tr_ops = {
357
.disas_log = mips_tr_disas_log,
358
};
359
360
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
361
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
362
+ target_ulong pc, void *host_pc)
363
{
364
DisasContext ctx;
365
366
- translator_loop(&mips_tr_ops, &ctx.base, cs, tb, max_insns);
367
+ translator_loop(cs, tb, max_insns, pc, host_pc, &mips_tr_ops, &ctx.base);
368
}
369
370
void mips_tcg_init(void)
371
diff --git a/target/nios2/translate.c b/target/nios2/translate.c
372
index XXXXXXX..XXXXXXX 100644
373
--- a/target/nios2/translate.c
374
+++ b/target/nios2/translate.c
375
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps nios2_tr_ops = {
376
.disas_log = nios2_tr_disas_log,
377
};
378
379
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
380
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
381
+ target_ulong pc, void *host_pc)
382
{
383
DisasContext dc;
384
- translator_loop(&nios2_tr_ops, &dc.base, cs, tb, max_insns);
385
+ translator_loop(cs, tb, max_insns, pc, host_pc, &nios2_tr_ops, &dc.base);
386
}
387
388
void nios2_cpu_dump_state(CPUState *cs, FILE *f, int flags)
389
diff --git a/target/openrisc/translate.c b/target/openrisc/translate.c
390
index XXXXXXX..XXXXXXX 100644
391
--- a/target/openrisc/translate.c
392
+++ b/target/openrisc/translate.c
393
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps openrisc_tr_ops = {
394
.disas_log = openrisc_tr_disas_log,
395
};
396
397
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
398
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
399
+ target_ulong pc, void *host_pc)
400
{
401
DisasContext ctx;
402
403
- translator_loop(&openrisc_tr_ops, &ctx.base, cs, tb, max_insns);
404
+ translator_loop(cs, tb, max_insns, pc, host_pc,
405
+ &openrisc_tr_ops, &ctx.base);
406
}
407
408
void openrisc_cpu_dump_state(CPUState *cs, FILE *f, int flags)
409
diff --git a/target/ppc/translate.c b/target/ppc/translate.c
410
index XXXXXXX..XXXXXXX 100644
411
--- a/target/ppc/translate.c
412
+++ b/target/ppc/translate.c
413
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps ppc_tr_ops = {
414
.disas_log = ppc_tr_disas_log,
415
};
416
417
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
418
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
419
+ target_ulong pc, void *host_pc)
420
{
421
DisasContext ctx;
422
423
- translator_loop(&ppc_tr_ops, &ctx.base, cs, tb, max_insns);
424
+ translator_loop(cs, tb, max_insns, pc, host_pc, &ppc_tr_ops, &ctx.base);
425
}
426
427
void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb,
428
diff --git a/target/riscv/translate.c b/target/riscv/translate.c
429
index XXXXXXX..XXXXXXX 100644
430
--- a/target/riscv/translate.c
431
+++ b/target/riscv/translate.c
432
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps riscv_tr_ops = {
433
.disas_log = riscv_tr_disas_log,
434
};
435
436
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
437
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
438
+ target_ulong pc, void *host_pc)
439
{
440
DisasContext ctx;
441
442
- translator_loop(&riscv_tr_ops, &ctx.base, cs, tb, max_insns);
443
+ translator_loop(cs, tb, max_insns, pc, host_pc, &riscv_tr_ops, &ctx.base);
444
}
445
446
void riscv_translate_init(void)
447
diff --git a/target/rx/translate.c b/target/rx/translate.c
448
index XXXXXXX..XXXXXXX 100644
449
--- a/target/rx/translate.c
450
+++ b/target/rx/translate.c
451
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps rx_tr_ops = {
452
.disas_log = rx_tr_disas_log,
453
};
454
455
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
456
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
457
+ target_ulong pc, void *host_pc)
458
{
459
DisasContext dc;
460
461
- translator_loop(&rx_tr_ops, &dc.base, cs, tb, max_insns);
462
+ translator_loop(cs, tb, max_insns, pc, host_pc, &rx_tr_ops, &dc.base);
463
}
464
465
void restore_state_to_opc(CPURXState *env, TranslationBlock *tb,
466
diff --git a/target/s390x/tcg/translate.c b/target/s390x/tcg/translate.c
467
index XXXXXXX..XXXXXXX 100644
468
--- a/target/s390x/tcg/translate.c
469
+++ b/target/s390x/tcg/translate.c
470
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps s390x_tr_ops = {
471
.disas_log = s390x_tr_disas_log,
472
};
473
474
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
475
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
476
+ target_ulong pc, void *host_pc)
477
{
478
DisasContext dc;
479
480
- translator_loop(&s390x_tr_ops, &dc.base, cs, tb, max_insns);
481
+ translator_loop(cs, tb, max_insns, pc, host_pc, &s390x_tr_ops, &dc.base);
482
}
483
484
void restore_state_to_opc(CPUS390XState *env, TranslationBlock *tb,
485
diff --git a/target/sh4/translate.c b/target/sh4/translate.c
486
index XXXXXXX..XXXXXXX 100644
487
--- a/target/sh4/translate.c
488
+++ b/target/sh4/translate.c
489
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps sh4_tr_ops = {
490
.disas_log = sh4_tr_disas_log,
491
};
492
493
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
494
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
495
+ target_ulong pc, void *host_pc)
496
{
497
DisasContext ctx;
498
499
- translator_loop(&sh4_tr_ops, &ctx.base, cs, tb, max_insns);
500
+ translator_loop(cs, tb, max_insns, pc, host_pc, &sh4_tr_ops, &ctx.base);
501
}
502
503
void restore_state_to_opc(CPUSH4State *env, TranslationBlock *tb,
504
diff --git a/target/sparc/translate.c b/target/sparc/translate.c
505
index XXXXXXX..XXXXXXX 100644
506
--- a/target/sparc/translate.c
507
+++ b/target/sparc/translate.c
508
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps sparc_tr_ops = {
509
.disas_log = sparc_tr_disas_log,
510
};
511
512
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
513
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
514
+ target_ulong pc, void *host_pc)
515
{
516
DisasContext dc = {};
517
518
- translator_loop(&sparc_tr_ops, &dc.base, cs, tb, max_insns);
519
+ translator_loop(cs, tb, max_insns, pc, host_pc, &sparc_tr_ops, &dc.base);
520
}
521
522
void sparc_tcg_init(void)
523
diff --git a/target/tricore/translate.c b/target/tricore/translate.c
524
index XXXXXXX..XXXXXXX 100644
525
--- a/target/tricore/translate.c
526
+++ b/target/tricore/translate.c
527
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps tricore_tr_ops = {
528
};
529
530
531
-void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
532
+void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
533
+ target_ulong pc, void *host_pc)
534
{
535
DisasContext ctx;
536
- translator_loop(&tricore_tr_ops, &ctx.base, cs, tb, max_insns);
537
+ translator_loop(cs, tb, max_insns, pc, host_pc,
538
+ &tricore_tr_ops, &ctx.base);
539
}
540
541
void
542
diff --git a/target/xtensa/translate.c b/target/xtensa/translate.c
543
index XXXXXXX..XXXXXXX 100644
544
--- a/target/xtensa/translate.c
545
+++ b/target/xtensa/translate.c
546
@@ -XXX,XX +XXX,XX @@ static const TranslatorOps xtensa_translator_ops = {
547
.disas_log = xtensa_tr_disas_log,
548
};
549
550
-void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns)
551
+void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns,
552
+ target_ulong pc, void *host_pc)
553
{
554
DisasContext dc = {};
555
- translator_loop(&xtensa_translator_ops, &dc.base, cpu, tb, max_insns);
556
+ translator_loop(cpu, tb, max_insns, pc, host_pc,
557
+ &xtensa_translator_ops, &dc.base);
558
}
559
560
void xtensa_cpu_dump_state(CPUState *cs, FILE *f, int flags)
65
--
561
--
66
2.17.1
562
2.34.1
67
68
diff view generated by jsdifflib
1
All callers pass false to this argument. Remove it and pass the
1
Cache the translation from guest to host address, so we may
2
constant on to tb_invalidate_phys_page_range__locked.
2
use direct loads when we hit on the primary translation page.
3
3
4
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
4
Look up the second translation page only once, during translation.
5
Reviewed-by: David Hildenbrand <david@redhat.com>
5
This obviates another lookup of the second page within tb_gen_code
6
after translation.
7
8
Fixes a bug in that plugin_insn_append should be passed the bytes
9
in the original memory order, not bswapped by pieces.
10
11
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
12
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
6
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
13
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
7
---
14
---
8
accel/tcg/translate-all.h | 3 +--
15
include/exec/translator.h | 63 +++++++++++--------
9
accel/tcg/translate-all.c | 6 ++----
16
accel/tcg/translate-all.c | 23 +++----
10
exec.c | 4 ++--
17
accel/tcg/translator.c | 126 +++++++++++++++++++++++++++++---------
11
3 files changed, 5 insertions(+), 8 deletions(-)
18
3 files changed, 141 insertions(+), 71 deletions(-)
12
19
13
diff --git a/accel/tcg/translate-all.h b/accel/tcg/translate-all.h
20
diff --git a/include/exec/translator.h b/include/exec/translator.h
14
index XXXXXXX..XXXXXXX 100644
21
index XXXXXXX..XXXXXXX 100644
15
--- a/accel/tcg/translate-all.h
22
--- a/include/exec/translator.h
16
+++ b/accel/tcg/translate-all.h
23
+++ b/include/exec/translator.h
17
@@ -XXX,XX +XXX,XX @@ struct page_collection *page_collection_lock(tb_page_addr_t start,
24
@@ -XXX,XX +XXX,XX @@ typedef enum DisasJumpType {
18
void page_collection_unlock(struct page_collection *set);
25
* Architecture-agnostic disassembly context.
19
void tb_invalidate_phys_page_fast(struct page_collection *pages,
26
*/
20
tb_page_addr_t start, int len);
27
typedef struct DisasContextBase {
21
-void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
28
- const TranslationBlock *tb;
22
- int is_cpu_write_access);
29
+ TranslationBlock *tb;
23
+void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end);
30
target_ulong pc_first;
24
void tb_check_watchpoint(CPUState *cpu);
31
target_ulong pc_next;
25
32
DisasJumpType is_jmp;
26
#ifdef CONFIG_USER_ONLY
33
int num_insns;
34
int max_insns;
35
bool singlestep_enabled;
36
-#ifdef CONFIG_USER_ONLY
37
- /*
38
- * Guest address of the last byte of the last protected page.
39
- *
40
- * Pages containing the translated instructions are made non-writable in
41
- * order to achieve consistency in case another thread is modifying the
42
- * code while translate_insn() fetches the instruction bytes piecemeal.
43
- * Such writer threads are blocked on mmap_lock() in page_unprotect().
44
- */
45
- target_ulong page_protect_end;
46
-#endif
47
+ void *host_addr[2];
48
} DisasContextBase;
49
50
/**
51
@@ -XXX,XX +XXX,XX @@ bool translator_use_goto_tb(DisasContextBase *db, target_ulong dest);
52
* the relevant information at translation time.
53
*/
54
55
-#define GEN_TRANSLATOR_LD(fullname, type, load_fn, swap_fn) \
56
- type fullname ## _swap(CPUArchState *env, DisasContextBase *dcbase, \
57
- abi_ptr pc, bool do_swap); \
58
- static inline type fullname(CPUArchState *env, \
59
- DisasContextBase *dcbase, abi_ptr pc) \
60
- { \
61
- return fullname ## _swap(env, dcbase, pc, false); \
62
+uint8_t translator_ldub(CPUArchState *env, DisasContextBase *db, abi_ptr pc);
63
+uint16_t translator_lduw(CPUArchState *env, DisasContextBase *db, abi_ptr pc);
64
+uint32_t translator_ldl(CPUArchState *env, DisasContextBase *db, abi_ptr pc);
65
+uint64_t translator_ldq(CPUArchState *env, DisasContextBase *db, abi_ptr pc);
66
+
67
+static inline uint16_t
68
+translator_lduw_swap(CPUArchState *env, DisasContextBase *db,
69
+ abi_ptr pc, bool do_swap)
70
+{
71
+ uint16_t ret = translator_lduw(env, db, pc);
72
+ if (do_swap) {
73
+ ret = bswap16(ret);
74
}
75
+ return ret;
76
+}
77
78
-#define FOR_EACH_TRANSLATOR_LD(F) \
79
- F(translator_ldub, uint8_t, cpu_ldub_code, /* no swap */) \
80
- F(translator_lduw, uint16_t, cpu_lduw_code, bswap16) \
81
- F(translator_ldl, uint32_t, cpu_ldl_code, bswap32) \
82
- F(translator_ldq, uint64_t, cpu_ldq_code, bswap64)
83
+static inline uint32_t
84
+translator_ldl_swap(CPUArchState *env, DisasContextBase *db,
85
+ abi_ptr pc, bool do_swap)
86
+{
87
+ uint32_t ret = translator_ldl(env, db, pc);
88
+ if (do_swap) {
89
+ ret = bswap32(ret);
90
+ }
91
+ return ret;
92
+}
93
94
-FOR_EACH_TRANSLATOR_LD(GEN_TRANSLATOR_LD)
95
-
96
-#undef GEN_TRANSLATOR_LD
97
+static inline uint64_t
98
+translator_ldq_swap(CPUArchState *env, DisasContextBase *db,
99
+ abi_ptr pc, bool do_swap)
100
+{
101
+ uint64_t ret = translator_ldq_swap(env, db, pc, false);
102
+ if (do_swap) {
103
+ ret = bswap64(ret);
104
+ }
105
+ return ret;
106
+}
107
108
/*
109
* Return whether addr is on the same page as where disassembly started.
27
diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c
110
diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c
28
index XXXXXXX..XXXXXXX 100644
111
index XXXXXXX..XXXXXXX 100644
29
--- a/accel/tcg/translate-all.c
112
--- a/accel/tcg/translate-all.c
30
+++ b/accel/tcg/translate-all.c
113
+++ b/accel/tcg/translate-all.c
31
@@ -XXX,XX +XXX,XX @@ tb_invalidate_phys_page_range__locked(struct page_collection *pages,
114
@@ -XXX,XX +XXX,XX @@ TranslationBlock *tb_gen_code(CPUState *cpu,
32
*
33
* Called with mmap_lock held for user-mode emulation
34
*/
35
-void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
36
- int is_cpu_write_access)
37
+void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end)
38
{
115
{
39
struct page_collection *pages;
116
CPUArchState *env = cpu->env_ptr;
40
PageDesc *p;
117
TranslationBlock *tb, *existing_tb;
41
@@ -XXX,XX +XXX,XX @@ void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
118
- tb_page_addr_t phys_pc, phys_page2;
42
return;
119
- target_ulong virt_page2;
43
}
120
+ tb_page_addr_t phys_pc;
44
pages = page_collection_lock(start, end);
121
tcg_insn_unit *gen_code_buf;
45
- tb_invalidate_phys_page_range__locked(pages, p, start, end,
122
int gen_code_size, search_size, max_insns;
46
- is_cpu_write_access);
123
#ifdef CONFIG_PROFILER
47
+ tb_invalidate_phys_page_range__locked(pages, p, start, end, 0);
124
@@ -XXX,XX +XXX,XX @@ TranslationBlock *tb_gen_code(CPUState *cpu,
48
page_collection_unlock(pages);
125
tb->flags = flags;
126
tb->cflags = cflags;
127
tb->trace_vcpu_dstate = *cpu->trace_dstate;
128
+ tb->page_addr[0] = phys_pc;
129
+ tb->page_addr[1] = -1;
130
tcg_ctx->tb_cflags = cflags;
131
tb_overflow:
132
133
@@ -XXX,XX +XXX,XX @@ TranslationBlock *tb_gen_code(CPUState *cpu,
134
}
135
136
/*
137
- * If the TB is not associated with a physical RAM page then
138
- * it must be a temporary one-insn TB, and we have nothing to do
139
- * except fill in the page_addr[] fields. Return early before
140
- * attempting to link to other TBs or add to the lookup table.
141
+ * If the TB is not associated with a physical RAM page then it must be
142
+ * a temporary one-insn TB, and we have nothing left to do. Return early
143
+ * before attempting to link to other TBs or add to the lookup table.
144
*/
145
- if (phys_pc == -1) {
146
- tb->page_addr[0] = tb->page_addr[1] = -1;
147
+ if (tb->page_addr[0] == -1) {
148
return tb;
149
}
150
151
@@ -XXX,XX +XXX,XX @@ TranslationBlock *tb_gen_code(CPUState *cpu,
152
*/
153
tcg_tb_insert(tb);
154
155
- /* check next page if needed */
156
- virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
157
- phys_page2 = -1;
158
- if ((pc & TARGET_PAGE_MASK) != virt_page2) {
159
- phys_page2 = get_page_addr_code(env, virt_page2);
160
- }
161
/*
162
* No explicit memory barrier is required -- tb_link_page() makes the
163
* TB visible in a consistent state.
164
*/
165
- existing_tb = tb_link_page(tb, phys_pc, phys_page2);
166
+ existing_tb = tb_link_page(tb, tb->page_addr[0], tb->page_addr[1]);
167
/* if the TB already exists, discard what we just translated */
168
if (unlikely(existing_tb != tb)) {
169
uintptr_t orig_aligned = (uintptr_t)gen_code_buf;
170
diff --git a/accel/tcg/translator.c b/accel/tcg/translator.c
171
index XXXXXXX..XXXXXXX 100644
172
--- a/accel/tcg/translator.c
173
+++ b/accel/tcg/translator.c
174
@@ -XXX,XX +XXX,XX @@ bool translator_use_goto_tb(DisasContextBase *db, target_ulong dest)
175
return ((db->pc_first ^ dest) & TARGET_PAGE_MASK) == 0;
49
}
176
}
50
177
51
diff --git a/exec.c b/exec.c
178
-static inline void translator_page_protect(DisasContextBase *dcbase,
52
index XXXXXXX..XXXXXXX 100644
179
- target_ulong pc)
53
--- a/exec.c
180
-{
54
+++ b/exec.c
181
-#ifdef CONFIG_USER_ONLY
55
@@ -XXX,XX +XXX,XX @@ const char *parse_cpu_option(const char *cpu_option)
182
- dcbase->page_protect_end = pc | ~TARGET_PAGE_MASK;
56
void tb_invalidate_phys_addr(target_ulong addr)
183
- page_protect(pc);
184
-#endif
185
-}
186
-
187
void translator_loop(CPUState *cpu, TranslationBlock *tb, int max_insns,
188
target_ulong pc, void *host_pc,
189
const TranslatorOps *ops, DisasContextBase *db)
190
@@ -XXX,XX +XXX,XX @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int max_insns,
191
db->num_insns = 0;
192
db->max_insns = max_insns;
193
db->singlestep_enabled = cflags & CF_SINGLE_STEP;
194
- translator_page_protect(db, db->pc_next);
195
+ db->host_addr[0] = host_pc;
196
+ db->host_addr[1] = NULL;
197
+
198
+#ifdef CONFIG_USER_ONLY
199
+ page_protect(pc);
200
+#endif
201
202
ops->init_disas_context(db, cpu);
203
tcg_debug_assert(db->is_jmp == DISAS_NEXT); /* no early exit */
204
@@ -XXX,XX +XXX,XX @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int max_insns,
205
#endif
206
}
207
208
-static inline void translator_maybe_page_protect(DisasContextBase *dcbase,
209
- target_ulong pc, size_t len)
210
+static void *translator_access(CPUArchState *env, DisasContextBase *db,
211
+ target_ulong pc, size_t len)
57
{
212
{
58
mmap_lock();
213
-#ifdef CONFIG_USER_ONLY
59
- tb_invalidate_phys_page_range(addr, addr + 1, 0);
214
- target_ulong end = pc + len - 1;
60
+ tb_invalidate_phys_page_range(addr, addr + 1);
215
+ void *host;
61
mmap_unlock();
216
+ target_ulong base, end;
217
+ TranslationBlock *tb;
218
219
- if (end > dcbase->page_protect_end) {
220
- translator_page_protect(dcbase, end);
221
+ tb = db->tb;
222
+
223
+ /* Use slow path if first page is MMIO. */
224
+ if (unlikely(tb->page_addr[0] == -1)) {
225
+ return NULL;
226
}
227
+
228
+ end = pc + len - 1;
229
+ if (likely(is_same_page(db, end))) {
230
+ host = db->host_addr[0];
231
+ base = db->pc_first;
232
+ } else {
233
+ host = db->host_addr[1];
234
+ base = TARGET_PAGE_ALIGN(db->pc_first);
235
+ if (host == NULL) {
236
+ tb->page_addr[1] =
237
+ get_page_addr_code_hostp(env, base, &db->host_addr[1]);
238
+#ifdef CONFIG_USER_ONLY
239
+ page_protect(end);
240
#endif
241
+ /* We cannot handle MMIO as second page. */
242
+ assert(tb->page_addr[1] != -1);
243
+ host = db->host_addr[1];
244
+ }
245
+
246
+ /* Use slow path when crossing pages. */
247
+ if (is_same_page(db, pc)) {
248
+ return NULL;
249
+ }
250
+ }
251
+
252
+ tcg_debug_assert(pc >= base);
253
+ return host + (pc - base);
62
}
254
}
63
255
64
@@ -XXX,XX +XXX,XX @@ void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr, MemTxAttrs attrs)
256
-#define GEN_TRANSLATOR_LD(fullname, type, load_fn, swap_fn) \
65
return;
257
- type fullname ## _swap(CPUArchState *env, DisasContextBase *dcbase, \
66
}
258
- abi_ptr pc, bool do_swap) \
67
ram_addr = memory_region_get_ram_addr(mr) + addr;
259
- { \
68
- tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
260
- translator_maybe_page_protect(dcbase, pc, sizeof(type)); \
69
+ tb_invalidate_phys_page_range(ram_addr, ram_addr + 1);
261
- type ret = load_fn(env, pc); \
70
rcu_read_unlock();
262
- if (do_swap) { \
71
}
263
- ret = swap_fn(ret); \
72
264
- } \
265
- plugin_insn_append(pc, &ret, sizeof(ret)); \
266
- return ret; \
267
+uint8_t translator_ldub(CPUArchState *env, DisasContextBase *db, abi_ptr pc)
268
+{
269
+ uint8_t ret;
270
+ void *p = translator_access(env, db, pc, sizeof(ret));
271
+
272
+ if (p) {
273
+ plugin_insn_append(pc, p, sizeof(ret));
274
+ return ldub_p(p);
275
}
276
+ ret = cpu_ldub_code(env, pc);
277
+ plugin_insn_append(pc, &ret, sizeof(ret));
278
+ return ret;
279
+}
280
281
-FOR_EACH_TRANSLATOR_LD(GEN_TRANSLATOR_LD)
282
+uint16_t translator_lduw(CPUArchState *env, DisasContextBase *db, abi_ptr pc)
283
+{
284
+ uint16_t ret, plug;
285
+ void *p = translator_access(env, db, pc, sizeof(ret));
286
287
-#undef GEN_TRANSLATOR_LD
288
+ if (p) {
289
+ plugin_insn_append(pc, p, sizeof(ret));
290
+ return lduw_p(p);
291
+ }
292
+ ret = cpu_lduw_code(env, pc);
293
+ plug = tswap16(ret);
294
+ plugin_insn_append(pc, &plug, sizeof(ret));
295
+ return ret;
296
+}
297
+
298
+uint32_t translator_ldl(CPUArchState *env, DisasContextBase *db, abi_ptr pc)
299
+{
300
+ uint32_t ret, plug;
301
+ void *p = translator_access(env, db, pc, sizeof(ret));
302
+
303
+ if (p) {
304
+ plugin_insn_append(pc, p, sizeof(ret));
305
+ return ldl_p(p);
306
+ }
307
+ ret = cpu_ldl_code(env, pc);
308
+ plug = tswap32(ret);
309
+ plugin_insn_append(pc, &plug, sizeof(ret));
310
+ return ret;
311
+}
312
+
313
+uint64_t translator_ldq(CPUArchState *env, DisasContextBase *db, abi_ptr pc)
314
+{
315
+ uint64_t ret, plug;
316
+ void *p = translator_access(env, db, pc, sizeof(ret));
317
+
318
+ if (p) {
319
+ plugin_insn_append(pc, p, sizeof(ret));
320
+ return ldq_p(p);
321
+ }
322
+ ret = cpu_ldq_code(env, pc);
323
+ plug = tswap64(ret);
324
+ plugin_insn_append(pc, &plug, sizeof(ret));
325
+ return ret;
326
+}
73
--
327
--
74
2.17.1
328
2.34.1
75
76
diff view generated by jsdifflib
New patch
1
From: Ilya Leoshkevich <iii@linux.ibm.com>
1
2
3
Right now translator stops right *after* the end of a page, which
4
breaks reporting of fault locations when the last instruction of a
5
multi-insn translation block crosses a page boundary.
6
7
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
8
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
9
Message-Id: <20220817150506.592862-3-iii@linux.ibm.com>
10
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
11
---
12
target/s390x/tcg/translate.c | 15 +++-
13
tests/tcg/s390x/noexec.c | 106 +++++++++++++++++++++++
14
tests/tcg/multiarch/noexec.c.inc | 139 +++++++++++++++++++++++++++++++
15
tests/tcg/s390x/Makefile.target | 1 +
16
4 files changed, 257 insertions(+), 4 deletions(-)
17
create mode 100644 tests/tcg/s390x/noexec.c
18
create mode 100644 tests/tcg/multiarch/noexec.c.inc
19
20
diff --git a/target/s390x/tcg/translate.c b/target/s390x/tcg/translate.c
21
index XXXXXXX..XXXXXXX 100644
22
--- a/target/s390x/tcg/translate.c
23
+++ b/target/s390x/tcg/translate.c
24
@@ -XXX,XX +XXX,XX @@ static void s390x_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
25
dc->insn_start = tcg_last_op();
26
}
27
28
+static target_ulong get_next_pc(CPUS390XState *env, DisasContext *s,
29
+ uint64_t pc)
30
+{
31
+ uint64_t insn = ld_code2(env, s, pc);
32
+
33
+ return pc + get_ilen((insn >> 8) & 0xff);
34
+}
35
+
36
static void s390x_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
37
{
38
CPUS390XState *env = cs->env_ptr;
39
@@ -XXX,XX +XXX,XX @@ static void s390x_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
40
41
dc->base.is_jmp = translate_one(env, dc);
42
if (dc->base.is_jmp == DISAS_NEXT) {
43
- uint64_t page_start;
44
-
45
- page_start = dc->base.pc_first & TARGET_PAGE_MASK;
46
- if (dc->base.pc_next - page_start >= TARGET_PAGE_SIZE || dc->ex_value) {
47
+ if (!is_same_page(dcbase, dc->base.pc_next) ||
48
+ !is_same_page(dcbase, get_next_pc(env, dc, dc->base.pc_next)) ||
49
+ dc->ex_value) {
50
dc->base.is_jmp = DISAS_TOO_MANY;
51
}
52
}
53
diff --git a/tests/tcg/s390x/noexec.c b/tests/tcg/s390x/noexec.c
54
new file mode 100644
55
index XXXXXXX..XXXXXXX
56
--- /dev/null
57
+++ b/tests/tcg/s390x/noexec.c
58
@@ -XXX,XX +XXX,XX @@
59
+#include "../multiarch/noexec.c.inc"
60
+
61
+static void *arch_mcontext_pc(const mcontext_t *ctx)
62
+{
63
+ return (void *)ctx->psw.addr;
64
+}
65
+
66
+static int arch_mcontext_arg(const mcontext_t *ctx)
67
+{
68
+ return ctx->gregs[2];
69
+}
70
+
71
+static void arch_flush(void *p, int len)
72
+{
73
+}
74
+
75
+extern char noexec_1[];
76
+extern char noexec_2[];
77
+extern char noexec_end[];
78
+
79
+asm("noexec_1:\n"
80
+ " lgfi %r2,1\n" /* %r2 is 0 on entry, set 1. */
81
+ "noexec_2:\n"
82
+ " lgfi %r2,2\n" /* %r2 is 0/1; set 2. */
83
+ " br %r14\n" /* return */
84
+ "noexec_end:");
85
+
86
+extern char exrl_1[];
87
+extern char exrl_2[];
88
+extern char exrl_end[];
89
+
90
+asm("exrl_1:\n"
91
+ " exrl %r0, exrl_2\n"
92
+ " br %r14\n"
93
+ "exrl_2:\n"
94
+ " lgfi %r2,2\n"
95
+ "exrl_end:");
96
+
97
+int main(void)
98
+{
99
+ struct noexec_test noexec_tests[] = {
100
+ {
101
+ .name = "fallthrough",
102
+ .test_code = noexec_1,
103
+ .test_len = noexec_end - noexec_1,
104
+ .page_ofs = noexec_1 - noexec_2,
105
+ .entry_ofs = noexec_1 - noexec_2,
106
+ .expected_si_ofs = 0,
107
+ .expected_pc_ofs = 0,
108
+ .expected_arg = 1,
109
+ },
110
+ {
111
+ .name = "jump",
112
+ .test_code = noexec_1,
113
+ .test_len = noexec_end - noexec_1,
114
+ .page_ofs = noexec_1 - noexec_2,
115
+ .entry_ofs = 0,
116
+ .expected_si_ofs = 0,
117
+ .expected_pc_ofs = 0,
118
+ .expected_arg = 0,
119
+ },
120
+ {
121
+ .name = "exrl",
122
+ .test_code = exrl_1,
123
+ .test_len = exrl_end - exrl_1,
124
+ .page_ofs = exrl_1 - exrl_2,
125
+ .entry_ofs = exrl_1 - exrl_2,
126
+ .expected_si_ofs = 0,
127
+ .expected_pc_ofs = exrl_1 - exrl_2,
128
+ .expected_arg = 0,
129
+ },
130
+ {
131
+ .name = "fallthrough [cross]",
132
+ .test_code = noexec_1,
133
+ .test_len = noexec_end - noexec_1,
134
+ .page_ofs = noexec_1 - noexec_2 - 2,
135
+ .entry_ofs = noexec_1 - noexec_2 - 2,
136
+ .expected_si_ofs = 0,
137
+ .expected_pc_ofs = -2,
138
+ .expected_arg = 1,
139
+ },
140
+ {
141
+ .name = "jump [cross]",
142
+ .test_code = noexec_1,
143
+ .test_len = noexec_end - noexec_1,
144
+ .page_ofs = noexec_1 - noexec_2 - 2,
145
+ .entry_ofs = -2,
146
+ .expected_si_ofs = 0,
147
+ .expected_pc_ofs = -2,
148
+ .expected_arg = 0,
149
+ },
150
+ {
151
+ .name = "exrl [cross]",
152
+ .test_code = exrl_1,
153
+ .test_len = exrl_end - exrl_1,
154
+ .page_ofs = exrl_1 - exrl_2 - 2,
155
+ .entry_ofs = exrl_1 - exrl_2 - 2,
156
+ .expected_si_ofs = 0,
157
+ .expected_pc_ofs = exrl_1 - exrl_2 - 2,
158
+ .expected_arg = 0,
159
+ },
160
+ };
161
+
162
+ return test_noexec(noexec_tests,
163
+ sizeof(noexec_tests) / sizeof(noexec_tests[0]));
164
+}
165
diff --git a/tests/tcg/multiarch/noexec.c.inc b/tests/tcg/multiarch/noexec.c.inc
166
new file mode 100644
167
index XXXXXXX..XXXXXXX
168
--- /dev/null
169
+++ b/tests/tcg/multiarch/noexec.c.inc
170
@@ -XXX,XX +XXX,XX @@
171
+/*
172
+ * Common code for arch-specific MMU_INST_FETCH fault testing.
173
+ */
174
+
175
+#define _GNU_SOURCE
176
+
177
+#include <assert.h>
178
+#include <signal.h>
179
+#include <stdio.h>
180
+#include <stdlib.h>
181
+#include <string.h>
182
+#include <errno.h>
183
+#include <unistd.h>
184
+#include <sys/mman.h>
185
+#include <sys/ucontext.h>
186
+
187
+/* Forward declarations. */
188
+
189
+static void *arch_mcontext_pc(const mcontext_t *ctx);
190
+static int arch_mcontext_arg(const mcontext_t *ctx);
191
+static void arch_flush(void *p, int len);
192
+
193
+/* Testing infrastructure. */
194
+
195
+struct noexec_test {
196
+ const char *name;
197
+ const char *test_code;
198
+ int test_len;
199
+ int page_ofs;
200
+ int entry_ofs;
201
+ int expected_si_ofs;
202
+ int expected_pc_ofs;
203
+ int expected_arg;
204
+};
205
+
206
+static void *page_base;
207
+static int page_size;
208
+static const struct noexec_test *current_noexec_test;
209
+
210
+static void handle_err(const char *syscall)
211
+{
212
+ printf("[ FAILED ] %s: %s\n", syscall, strerror(errno));
213
+ exit(EXIT_FAILURE);
214
+}
215
+
216
+static void handle_segv(int sig, siginfo_t *info, void *ucontext)
217
+{
218
+ const struct noexec_test *test = current_noexec_test;
219
+ const mcontext_t *mc = &((ucontext_t *)ucontext)->uc_mcontext;
220
+ void *expected_si;
221
+ void *expected_pc;
222
+ void *pc;
223
+ int arg;
224
+
225
+ if (test == NULL) {
226
+ printf("[ FAILED ] unexpected SEGV\n");
227
+ exit(EXIT_FAILURE);
228
+ }
229
+ current_noexec_test = NULL;
230
+
231
+ expected_si = page_base + test->expected_si_ofs;
232
+ if (info->si_addr != expected_si) {
233
+ printf("[ FAILED ] wrong si_addr (%p != %p)\n",
234
+ info->si_addr, expected_si);
235
+ exit(EXIT_FAILURE);
236
+ }
237
+
238
+ pc = arch_mcontext_pc(mc);
239
+ expected_pc = page_base + test->expected_pc_ofs;
240
+ if (pc != expected_pc) {
241
+ printf("[ FAILED ] wrong pc (%p != %p)\n", pc, expected_pc);
242
+ exit(EXIT_FAILURE);
243
+ }
244
+
245
+ arg = arch_mcontext_arg(mc);
246
+ if (arg != test->expected_arg) {
247
+ printf("[ FAILED ] wrong arg (%d != %d)\n", arg, test->expected_arg);
248
+ exit(EXIT_FAILURE);
249
+ }
250
+
251
+ if (mprotect(page_base, page_size,
252
+ PROT_READ | PROT_WRITE | PROT_EXEC) < 0) {
253
+ handle_err("mprotect");
254
+ }
255
+}
256
+
257
+static void test_noexec_1(const struct noexec_test *test)
258
+{
259
+ void *start = page_base + test->page_ofs;
260
+ void (*fn)(int arg) = page_base + test->entry_ofs;
261
+
262
+ memcpy(start, test->test_code, test->test_len);
263
+ arch_flush(start, test->test_len);
264
+
265
+ /* Trigger TB creation in order to test invalidation. */
266
+ fn(0);
267
+
268
+ if (mprotect(page_base, page_size, PROT_NONE) < 0) {
269
+ handle_err("mprotect");
270
+ }
271
+
272
+ /* Trigger SEGV and check that handle_segv() ran. */
273
+ current_noexec_test = test;
274
+ fn(0);
275
+ assert(current_noexec_test == NULL);
276
+}
277
+
278
+static int test_noexec(struct noexec_test *tests, size_t n_tests)
279
+{
280
+ struct sigaction act;
281
+ size_t i;
282
+
283
+ memset(&act, 0, sizeof(act));
284
+ act.sa_sigaction = handle_segv;
285
+ act.sa_flags = SA_SIGINFO;
286
+ if (sigaction(SIGSEGV, &act, NULL) < 0) {
287
+ handle_err("sigaction");
288
+ }
289
+
290
+ page_size = getpagesize();
291
+ page_base = mmap(NULL, 2 * page_size,
292
+ PROT_READ | PROT_WRITE | PROT_EXEC,
293
+ MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
294
+ if (page_base == MAP_FAILED) {
295
+ handle_err("mmap");
296
+ }
297
+ page_base += page_size;
298
+
299
+ for (i = 0; i < n_tests; i++) {
300
+ struct noexec_test *test = &tests[i];
301
+
302
+ printf("[ RUN ] %s\n", test->name);
303
+ test_noexec_1(test);
304
+ printf("[ OK ]\n");
305
+ }
306
+
307
+ printf("[ PASSED ]\n");
308
+ return EXIT_SUCCESS;
309
+}
310
diff --git a/tests/tcg/s390x/Makefile.target b/tests/tcg/s390x/Makefile.target
311
index XXXXXXX..XXXXXXX 100644
312
--- a/tests/tcg/s390x/Makefile.target
313
+++ b/tests/tcg/s390x/Makefile.target
314
@@ -XXX,XX +XXX,XX @@ TESTS+=shift
315
TESTS+=trap
316
TESTS+=signals-s390x
317
TESTS+=branch-relative-long
318
+TESTS+=noexec
319
320
Z14_TESTS=vfminmax
321
vfminmax: LDFLAGS+=-lm
322
--
323
2.34.1
diff view generated by jsdifflib
1
We will shortly be using these more than once.
1
From: Ilya Leoshkevich <iii@linux.ibm.com>
2
2
3
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
3
Right now translator stops right *after* the end of a page, which
4
Reviewed-by: David Hildenbrand <david@redhat.com>
4
breaks reporting of fault locations when the last instruction of a
5
multi-insn translation block crosses a page boundary.
6
7
An implementation, like the one arm and s390x have, would require an
8
i386 length disassembler, which is burdensome to maintain. Another
9
alternative would be to single-step at the end of a guest page, but
10
this may come with a performance impact.
11
12
Fix by snapshotting disassembly state and restoring it after we figure
13
out we crossed a page boundary. This includes rolling back cc_op
14
updates and emitted ops.
15
16
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
17
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
18
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1143
19
Message-Id: <20220817150506.592862-4-iii@linux.ibm.com>
20
[rth: Simplify end-of-insn cross-page checks.]
5
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
21
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
6
---
22
---
7
accel/tcg/cputlb.c | 107 +++++++++++++++++++++++----------------------
23
target/i386/tcg/translate.c | 64 ++++++++++++++++-----------
8
1 file changed, 55 insertions(+), 52 deletions(-)
24
tests/tcg/x86_64/noexec.c | 75 ++++++++++++++++++++++++++++++++
9
25
tests/tcg/x86_64/Makefile.target | 3 +-
10
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
26
3 files changed, 116 insertions(+), 26 deletions(-)
27
create mode 100644 tests/tcg/x86_64/noexec.c
28
29
diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c
11
index XXXXXXX..XXXXXXX 100644
30
index XXXXXXX..XXXXXXX 100644
12
--- a/accel/tcg/cputlb.c
31
--- a/target/i386/tcg/translate.c
13
+++ b/accel/tcg/cputlb.c
32
+++ b/target/i386/tcg/translate.c
14
@@ -XXX,XX +XXX,XX @@ static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr,
33
@@ -XXX,XX +XXX,XX @@ typedef struct DisasContext {
15
typedef uint64_t FullLoadHelper(CPUArchState *env, target_ulong addr,
34
TCGv_i64 tmp1_i64;
16
TCGMemOpIdx oi, uintptr_t retaddr);
35
17
36
sigjmp_buf jmpbuf;
18
+static inline uint64_t QEMU_ALWAYS_INLINE
37
+ TCGOp *prev_insn_end;
19
+load_memop(const void *haddr, MemOp op)
38
} DisasContext;
20
+{
39
21
+ switch (op) {
40
/* The environment in which user-only runs is constrained. */
22
+ case MO_UB:
41
@@ -XXX,XX +XXX,XX @@ static uint64_t advance_pc(CPUX86State *env, DisasContext *s, int num_bytes)
23
+ return ldub_p(haddr);
42
{
24
+ case MO_BEUW:
43
uint64_t pc = s->pc;
25
+ return lduw_be_p(haddr);
44
26
+ case MO_LEUW:
45
+ /* This is a subsequent insn that crosses a page boundary. */
27
+ return lduw_le_p(haddr);
46
+ if (s->base.num_insns > 1 &&
28
+ case MO_BEUL:
47
+ !is_same_page(&s->base, s->pc + num_bytes - 1)) {
29
+ return (uint32_t)ldl_be_p(haddr);
48
+ siglongjmp(s->jmpbuf, 2);
30
+ case MO_LEUL:
49
+ }
31
+ return (uint32_t)ldl_le_p(haddr);
50
+
32
+ case MO_BEQ:
51
s->pc += num_bytes;
33
+ return ldq_be_p(haddr);
52
if (unlikely(s->pc - s->pc_start > X86_MAX_INSN_LENGTH)) {
34
+ case MO_LEQ:
53
/* If the instruction's 16th byte is on a different page than the 1st, a
35
+ return ldq_le_p(haddr);
54
@@ -XXX,XX +XXX,XX @@ static target_ulong disas_insn(DisasContext *s, CPUState *cpu)
55
int modrm, reg, rm, mod, op, opreg, val;
56
target_ulong next_eip, tval;
57
target_ulong pc_start = s->base.pc_next;
58
+ bool orig_cc_op_dirty = s->cc_op_dirty;
59
+ CCOp orig_cc_op = s->cc_op;
60
61
s->pc_start = s->pc = pc_start;
62
s->override = -1;
63
@@ -XXX,XX +XXX,XX @@ static target_ulong disas_insn(DisasContext *s, CPUState *cpu)
64
s->rip_offset = 0; /* for relative ip address */
65
s->vex_l = 0;
66
s->vex_v = 0;
67
- if (sigsetjmp(s->jmpbuf, 0) != 0) {
68
+ switch (sigsetjmp(s->jmpbuf, 0)) {
69
+ case 0:
70
+ break;
71
+ case 1:
72
gen_exception_gpf(s);
73
return s->pc;
74
+ case 2:
75
+ /* Restore state that may affect the next instruction. */
76
+ s->cc_op_dirty = orig_cc_op_dirty;
77
+ s->cc_op = orig_cc_op;
78
+ s->base.num_insns--;
79
+ tcg_remove_ops_after(s->prev_insn_end);
80
+ s->base.is_jmp = DISAS_TOO_MANY;
81
+ return pc_start;
36
+ default:
82
+ default:
37
+ qemu_build_not_reached();
83
+ g_assert_not_reached();
38
+ }
84
}
39
+}
85
40
+
86
prefixes = 0;
41
static inline uint64_t QEMU_ALWAYS_INLINE
87
@@ -XXX,XX +XXX,XX @@ static void i386_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
42
load_helper(CPUArchState *env, target_ulong addr, TCGMemOpIdx oi,
88
{
43
uintptr_t retaddr, MemOp op, bool code_read,
89
DisasContext *dc = container_of(dcbase, DisasContext, base);
44
@@ -XXX,XX +XXX,XX @@ load_helper(CPUArchState *env, target_ulong addr, TCGMemOpIdx oi,
90
45
91
+ dc->prev_insn_end = tcg_last_op();
46
do_aligned_access:
92
tcg_gen_insn_start(dc->base.pc_next, dc->cc_op);
47
haddr = (void *)((uintptr_t)addr + entry->addend);
93
}
48
- switch (op) {
94
49
- case MO_UB:
95
@@ -XXX,XX +XXX,XX @@ static void i386_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
50
- res = ldub_p(haddr);
96
#endif
51
- break;
97
52
- case MO_BEUW:
98
pc_next = disas_insn(dc, cpu);
53
- res = lduw_be_p(haddr);
99
-
54
- break;
100
- if (dc->flags & (HF_TF_MASK | HF_INHIBIT_IRQ_MASK)) {
55
- case MO_LEUW:
101
- /* if single step mode, we generate only one instruction and
56
- res = lduw_le_p(haddr);
102
- generate an exception */
57
- break;
103
- /* if irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
58
- case MO_BEUL:
104
- the flag and abort the translation to give the irqs a
59
- res = (uint32_t)ldl_be_p(haddr);
105
- chance to happen */
60
- break;
106
- dc->base.is_jmp = DISAS_TOO_MANY;
61
- case MO_LEUL:
107
- } else if ((tb_cflags(dc->base.tb) & CF_USE_ICOUNT)
62
- res = (uint32_t)ldl_le_p(haddr);
108
- && ((pc_next & TARGET_PAGE_MASK)
63
- break;
109
- != ((pc_next + TARGET_MAX_INSN_SIZE - 1)
64
- case MO_BEQ:
110
- & TARGET_PAGE_MASK)
65
- res = ldq_be_p(haddr);
111
- || (pc_next & ~TARGET_PAGE_MASK) == 0)) {
66
- break;
112
- /* Do not cross the boundary of the pages in icount mode,
67
- case MO_LEQ:
113
- it can cause an exception. Do it only when boundary is
68
- res = ldq_le_p(haddr);
114
- crossed by the first instruction in the block.
69
- break;
115
- If current instruction already crossed the bound - it's ok,
70
- default:
116
- because an exception hasn't stopped this code.
71
- qemu_build_not_reached();
117
- */
118
- dc->base.is_jmp = DISAS_TOO_MANY;
119
- } else if ((pc_next - dc->base.pc_first) >= (TARGET_PAGE_SIZE - 32)) {
120
- dc->base.is_jmp = DISAS_TOO_MANY;
72
- }
121
- }
73
-
122
-
74
- return res;
123
dc->base.pc_next = pc_next;
75
+ return load_memop(haddr, op);
124
+
125
+ if (dc->base.is_jmp == DISAS_NEXT) {
126
+ if (dc->flags & (HF_TF_MASK | HF_INHIBIT_IRQ_MASK)) {
127
+ /*
128
+ * If single step mode, we generate only one instruction and
129
+ * generate an exception.
130
+ * If irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
131
+ * the flag and abort the translation to give the irqs a
132
+ * chance to happen.
133
+ */
134
+ dc->base.is_jmp = DISAS_TOO_MANY;
135
+ } else if (!is_same_page(&dc->base, pc_next)) {
136
+ dc->base.is_jmp = DISAS_TOO_MANY;
137
+ }
138
+ }
76
}
139
}
77
140
78
/*
141
static void i386_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
79
@@ -XXX,XX +XXX,XX @@ tcg_target_ulong helper_be_ldsl_mmu(CPUArchState *env, target_ulong addr,
142
diff --git a/tests/tcg/x86_64/noexec.c b/tests/tcg/x86_64/noexec.c
80
* Store Helpers
143
new file mode 100644
81
*/
144
index XXXXXXX..XXXXXXX
82
145
--- /dev/null
83
+static inline void QEMU_ALWAYS_INLINE
146
+++ b/tests/tcg/x86_64/noexec.c
84
+store_memop(void *haddr, uint64_t val, MemOp op)
147
@@ -XXX,XX +XXX,XX @@
85
+{
148
+#include "../multiarch/noexec.c.inc"
86
+ switch (op) {
149
+
87
+ case MO_UB:
150
+static void *arch_mcontext_pc(const mcontext_t *ctx)
88
+ stb_p(haddr, val);
151
+{
89
+ break;
152
+ return (void *)ctx->gregs[REG_RIP];
90
+ case MO_BEUW:
153
+}
91
+ stw_be_p(haddr, val);
154
+
92
+ break;
155
+int arch_mcontext_arg(const mcontext_t *ctx)
93
+ case MO_LEUW:
156
+{
94
+ stw_le_p(haddr, val);
157
+ return ctx->gregs[REG_RDI];
95
+ break;
158
+}
96
+ case MO_BEUL:
159
+
97
+ stl_be_p(haddr, val);
160
+static void arch_flush(void *p, int len)
98
+ break;
161
+{
99
+ case MO_LEUL:
162
+}
100
+ stl_le_p(haddr, val);
163
+
101
+ break;
164
+extern char noexec_1[];
102
+ case MO_BEQ:
165
+extern char noexec_2[];
103
+ stq_be_p(haddr, val);
166
+extern char noexec_end[];
104
+ break;
167
+
105
+ case MO_LEQ:
168
+asm("noexec_1:\n"
106
+ stq_le_p(haddr, val);
169
+ " movq $1,%rdi\n" /* %rdi is 0 on entry, set 1. */
107
+ break;
170
+ "noexec_2:\n"
108
+ default:
171
+ " movq $2,%rdi\n" /* %rdi is 0/1; set 2. */
109
+ qemu_build_not_reached();
172
+ " ret\n"
110
+ }
173
+ "noexec_end:");
111
+}
174
+
112
+
175
+int main(void)
113
static inline void QEMU_ALWAYS_INLINE
176
+{
114
store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
177
+ struct noexec_test noexec_tests[] = {
115
TCGMemOpIdx oi, uintptr_t retaddr, MemOp op)
178
+ {
116
@@ -XXX,XX +XXX,XX @@ store_helper(CPUArchState *env, target_ulong addr, uint64_t val,
179
+ .name = "fallthrough",
117
180
+ .test_code = noexec_1,
118
do_aligned_access:
181
+ .test_len = noexec_end - noexec_1,
119
haddr = (void *)((uintptr_t)addr + entry->addend);
182
+ .page_ofs = noexec_1 - noexec_2,
120
- switch (op) {
183
+ .entry_ofs = noexec_1 - noexec_2,
121
- case MO_UB:
184
+ .expected_si_ofs = 0,
122
- stb_p(haddr, val);
185
+ .expected_pc_ofs = 0,
123
- break;
186
+ .expected_arg = 1,
124
- case MO_BEUW:
187
+ },
125
- stw_be_p(haddr, val);
188
+ {
126
- break;
189
+ .name = "jump",
127
- case MO_LEUW:
190
+ .test_code = noexec_1,
128
- stw_le_p(haddr, val);
191
+ .test_len = noexec_end - noexec_1,
129
- break;
192
+ .page_ofs = noexec_1 - noexec_2,
130
- case MO_BEUL:
193
+ .entry_ofs = 0,
131
- stl_be_p(haddr, val);
194
+ .expected_si_ofs = 0,
132
- break;
195
+ .expected_pc_ofs = 0,
133
- case MO_LEUL:
196
+ .expected_arg = 0,
134
- stl_le_p(haddr, val);
197
+ },
135
- break;
198
+ {
136
- case MO_BEQ:
199
+ .name = "fallthrough [cross]",
137
- stq_be_p(haddr, val);
200
+ .test_code = noexec_1,
138
- break;
201
+ .test_len = noexec_end - noexec_1,
139
- case MO_LEQ:
202
+ .page_ofs = noexec_1 - noexec_2 - 2,
140
- stq_le_p(haddr, val);
203
+ .entry_ofs = noexec_1 - noexec_2 - 2,
141
- break;
204
+ .expected_si_ofs = 0,
142
- default:
205
+ .expected_pc_ofs = -2,
143
- qemu_build_not_reached();
206
+ .expected_arg = 1,
144
- }
207
+ },
145
+ store_memop(haddr, val, op);
208
+ {
146
}
209
+ .name = "jump [cross]",
147
210
+ .test_code = noexec_1,
148
void helper_ret_stb_mmu(CPUArchState *env, target_ulong addr, uint8_t val,
211
+ .test_len = noexec_end - noexec_1,
212
+ .page_ofs = noexec_1 - noexec_2 - 2,
213
+ .entry_ofs = -2,
214
+ .expected_si_ofs = 0,
215
+ .expected_pc_ofs = -2,
216
+ .expected_arg = 0,
217
+ },
218
+ };
219
+
220
+ return test_noexec(noexec_tests,
221
+ sizeof(noexec_tests) / sizeof(noexec_tests[0]));
222
+}
223
diff --git a/tests/tcg/x86_64/Makefile.target b/tests/tcg/x86_64/Makefile.target
224
index XXXXXXX..XXXXXXX 100644
225
--- a/tests/tcg/x86_64/Makefile.target
226
+++ b/tests/tcg/x86_64/Makefile.target
227
@@ -XXX,XX +XXX,XX @@ include $(SRC_PATH)/tests/tcg/i386/Makefile.target
228
229
ifeq ($(filter %-linux-user, $(TARGET)),$(TARGET))
230
X86_64_TESTS += vsyscall
231
+X86_64_TESTS += noexec
232
TESTS=$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64
233
else
234
TESTS=$(MULTIARCH_TESTS)
235
@@ -XXX,XX +XXX,XX @@ test-x86_64: LDFLAGS+=-lm -lc
236
test-x86_64: test-i386.c test-i386.h test-i386-shift.h test-i386-muldiv.h
237
    $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
238
239
-vsyscall: $(SRC_PATH)/tests/tcg/x86_64/vsyscall.c
240
+%: $(SRC_PATH)/tests/tcg/x86_64/%.c
241
    $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
149
--
242
--
150
2.17.1
243
2.34.1
151
152
diff view generated by jsdifflib
New patch
1
These will be useful in properly ending the TB.
1
2
3
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
4
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
5
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
6
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
7
---
8
target/riscv/translate.c | 10 +++++++++-
9
1 file changed, 9 insertions(+), 1 deletion(-)
10
11
diff --git a/target/riscv/translate.c b/target/riscv/translate.c
12
index XXXXXXX..XXXXXXX 100644
13
--- a/target/riscv/translate.c
14
+++ b/target/riscv/translate.c
15
@@ -XXX,XX +XXX,XX @@ static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
16
/* Include decoders for factored-out extensions */
17
#include "decode-XVentanaCondOps.c.inc"
18
19
+/* The specification allows for longer insns, but not supported by qemu. */
20
+#define MAX_INSN_LEN 4
21
+
22
+static inline int insn_len(uint16_t first_word)
23
+{
24
+ return (first_word & 3) == 3 ? 4 : 2;
25
+}
26
+
27
static void decode_opc(CPURISCVState *env, DisasContext *ctx, uint16_t opcode)
28
{
29
/*
30
@@ -XXX,XX +XXX,XX @@ static void decode_opc(CPURISCVState *env, DisasContext *ctx, uint16_t opcode)
31
};
32
33
/* Check for compressed insn */
34
- if (extract16(opcode, 0, 2) != 3) {
35
+ if (insn_len(opcode) == 2) {
36
if (!has_ext(ctx, RVC)) {
37
gen_exception_illegal(ctx);
38
} else {
39
--
40
2.34.1
diff view generated by jsdifflib
1
The memory_region_tb_read tracepoint is unreachable, since notdirty
1
Right now the translator stops right *after* the end of a page, which
2
is supposed to apply only to writes. The memory_region_tb_write
2
breaks reporting of fault locations when the last instruction of a
3
tracepoint is mis-named, because notdirty is not only used for TB
3
multi-insn translation block crosses a page boundary.
4
invalidation. It is also used for e.g. VGA RAM updates and migration.
5
4
6
Replace memory_region_tb_write with memory_notdirty_write_access,
5
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1155
7
and place it in memory_notdirty_write_prepare where it can catch
6
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
8
all of the instances. Add memory_notdirty_set_dirty to log when
7
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
9
we no longer intercept writes to a page.
8
Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
10
11
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
12
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
13
Reviewed-by: David Hildenbrand <david@redhat.com>
14
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
9
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
15
---
10
---
16
exec.c | 3 +++
11
target/riscv/translate.c | 17 +++++--
17
memory.c | 4 ----
12
tests/tcg/riscv64/noexec.c | 79 +++++++++++++++++++++++++++++++
18
trace-events | 4 ++--
13
tests/tcg/riscv64/Makefile.target | 1 +
19
3 files changed, 5 insertions(+), 6 deletions(-)
14
3 files changed, 93 insertions(+), 4 deletions(-)
15
create mode 100644 tests/tcg/riscv64/noexec.c
20
16
21
diff --git a/exec.c b/exec.c
17
diff --git a/target/riscv/translate.c b/target/riscv/translate.c
22
index XXXXXXX..XXXXXXX 100644
18
index XXXXXXX..XXXXXXX 100644
23
--- a/exec.c
19
--- a/target/riscv/translate.c
24
+++ b/exec.c
20
+++ b/target/riscv/translate.c
25
@@ -XXX,XX +XXX,XX @@ void memory_notdirty_write_prepare(NotDirtyInfo *ndi,
21
@@ -XXX,XX +XXX,XX @@ static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
26
ndi->size = size;
22
}
27
ndi->pages = NULL;
23
ctx->nftemp = 0;
28
24
29
+ trace_memory_notdirty_write_access(mem_vaddr, ram_addr, size);
25
+ /* Only the first insn within a TB is allowed to cross a page boundary. */
26
if (ctx->base.is_jmp == DISAS_NEXT) {
27
- target_ulong page_start;
28
-
29
- page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
30
- if (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE) {
31
+ if (!is_same_page(&ctx->base, ctx->base.pc_next)) {
32
ctx->base.is_jmp = DISAS_TOO_MANY;
33
+ } else {
34
+ unsigned page_ofs = ctx->base.pc_next & ~TARGET_PAGE_MASK;
30
+
35
+
31
assert(tcg_enabled());
36
+ if (page_ofs > TARGET_PAGE_SIZE - MAX_INSN_LEN) {
32
if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) {
37
+ uint16_t next_insn = cpu_lduw_code(env, ctx->base.pc_next);
33
ndi->pages = page_collection_lock(ram_addr, ram_addr + size);
38
+ int len = insn_len(next_insn);
34
@@ -XXX,XX +XXX,XX @@ void memory_notdirty_write_complete(NotDirtyInfo *ndi)
39
+
35
/* we remove the notdirty callback only if the code has been
40
+ if (!is_same_page(&ctx->base, ctx->base.pc_next + len)) {
36
flushed */
41
+ ctx->base.is_jmp = DISAS_TOO_MANY;
37
if (!cpu_physical_memory_is_clean(ndi->ram_addr)) {
42
+ }
38
+ trace_memory_notdirty_set_dirty(ndi->mem_vaddr);
43
+ }
39
tlb_set_dirty(ndi->cpu, ndi->mem_vaddr);
44
}
40
}
45
}
41
}
46
}
42
diff --git a/memory.c b/memory.c
47
diff --git a/tests/tcg/riscv64/noexec.c b/tests/tcg/riscv64/noexec.c
48
new file mode 100644
49
index XXXXXXX..XXXXXXX
50
--- /dev/null
51
+++ b/tests/tcg/riscv64/noexec.c
52
@@ -XXX,XX +XXX,XX @@
53
+#include "../multiarch/noexec.c.inc"
54
+
55
+static void *arch_mcontext_pc(const mcontext_t *ctx)
56
+{
57
+ return (void *)ctx->__gregs[REG_PC];
58
+}
59
+
60
+static int arch_mcontext_arg(const mcontext_t *ctx)
61
+{
62
+ return ctx->__gregs[REG_A0];
63
+}
64
+
65
+static void arch_flush(void *p, int len)
66
+{
67
+ __builtin___clear_cache(p, p + len);
68
+}
69
+
70
+extern char noexec_1[];
71
+extern char noexec_2[];
72
+extern char noexec_end[];
73
+
74
+asm(".option push\n"
75
+ ".option norvc\n"
76
+ "noexec_1:\n"
77
+ " li a0,1\n" /* a0 is 0 on entry, set 1. */
78
+ "noexec_2:\n"
79
+ " li a0,2\n" /* a0 is 0/1; set 2. */
80
+ " ret\n"
81
+ "noexec_end:\n"
82
+ ".option pop");
83
+
84
+int main(void)
85
+{
86
+ struct noexec_test noexec_tests[] = {
87
+ {
88
+ .name = "fallthrough",
89
+ .test_code = noexec_1,
90
+ .test_len = noexec_end - noexec_1,
91
+ .page_ofs = noexec_1 - noexec_2,
92
+ .entry_ofs = noexec_1 - noexec_2,
93
+ .expected_si_ofs = 0,
94
+ .expected_pc_ofs = 0,
95
+ .expected_arg = 1,
96
+ },
97
+ {
98
+ .name = "jump",
99
+ .test_code = noexec_1,
100
+ .test_len = noexec_end - noexec_1,
101
+ .page_ofs = noexec_1 - noexec_2,
102
+ .entry_ofs = 0,
103
+ .expected_si_ofs = 0,
104
+ .expected_pc_ofs = 0,
105
+ .expected_arg = 0,
106
+ },
107
+ {
108
+ .name = "fallthrough [cross]",
109
+ .test_code = noexec_1,
110
+ .test_len = noexec_end - noexec_1,
111
+ .page_ofs = noexec_1 - noexec_2 - 2,
112
+ .entry_ofs = noexec_1 - noexec_2 - 2,
113
+ .expected_si_ofs = 0,
114
+ .expected_pc_ofs = -2,
115
+ .expected_arg = 1,
116
+ },
117
+ {
118
+ .name = "jump [cross]",
119
+ .test_code = noexec_1,
120
+ .test_len = noexec_end - noexec_1,
121
+ .page_ofs = noexec_1 - noexec_2 - 2,
122
+ .entry_ofs = -2,
123
+ .expected_si_ofs = 0,
124
+ .expected_pc_ofs = -2,
125
+ .expected_arg = 0,
126
+ },
127
+ };
128
+
129
+ return test_noexec(noexec_tests,
130
+ sizeof(noexec_tests) / sizeof(noexec_tests[0]));
131
+}
132
diff --git a/tests/tcg/riscv64/Makefile.target b/tests/tcg/riscv64/Makefile.target
43
index XXXXXXX..XXXXXXX 100644
133
index XXXXXXX..XXXXXXX 100644
44
--- a/memory.c
134
--- a/tests/tcg/riscv64/Makefile.target
45
+++ b/memory.c
135
+++ b/tests/tcg/riscv64/Makefile.target
46
@@ -XXX,XX +XXX,XX @@ static MemTxResult memory_region_read_accessor(MemoryRegion *mr,
136
@@ -XXX,XX +XXX,XX @@
47
/* Accesses to code which has previously been translated into a TB show
137
48
* up in the MMIO path, as accesses to the io_mem_notdirty
138
VPATH += $(SRC_PATH)/tests/tcg/riscv64
49
* MemoryRegion. */
139
TESTS += test-div
50
- trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
140
+TESTS += noexec
51
} else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
52
hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
53
trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
54
@@ -XXX,XX +XXX,XX @@ static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr,
55
/* Accesses to code which has previously been translated into a TB show
56
* up in the MMIO path, as accesses to the io_mem_notdirty
57
* MemoryRegion. */
58
- trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
59
} else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
60
hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
61
trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
62
@@ -XXX,XX +XXX,XX @@ static MemTxResult memory_region_write_accessor(MemoryRegion *mr,
63
/* Accesses to code which has previously been translated into a TB show
64
* up in the MMIO path, as accesses to the io_mem_notdirty
65
* MemoryRegion. */
66
- trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size);
67
} else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
68
hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
69
trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
70
@@ -XXX,XX +XXX,XX @@ static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr,
71
/* Accesses to code which has previously been translated into a TB show
72
* up in the MMIO path, as accesses to the io_mem_notdirty
73
* MemoryRegion. */
74
- trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size);
75
} else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
76
hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
77
trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
78
diff --git a/trace-events b/trace-events
79
index XXXXXXX..XXXXXXX 100644
80
--- a/trace-events
81
+++ b/trace-events
82
@@ -XXX,XX +XXX,XX @@ dma_map_wait(void *dbs) "dbs=%p"
83
find_ram_offset(uint64_t size, uint64_t offset) "size: 0x%" PRIx64 " @ 0x%" PRIx64
84
find_ram_offset_loop(uint64_t size, uint64_t candidate, uint64_t offset, uint64_t next, uint64_t mingap) "trying size: 0x%" PRIx64 " @ 0x%" PRIx64 ", offset: 0x%" PRIx64" next: 0x%" PRIx64 " mingap: 0x%" PRIx64
85
ram_block_discard_range(const char *rbname, void *hva, size_t length, bool need_madvise, bool need_fallocate, int ret) "%s@%p + 0x%zx: madvise: %d fallocate: %d ret: %d"
86
+memory_notdirty_write_access(uint64_t vaddr, uint64_t ram_addr, unsigned size) "0x%" PRIx64 " ram_addr 0x%" PRIx64 " size %u"
87
+memory_notdirty_set_dirty(uint64_t vaddr) "0x%" PRIx64
88
89
# memory.c
90
memory_region_ops_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
91
memory_region_ops_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
92
memory_region_subpage_read(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u"
93
memory_region_subpage_write(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u"
94
-memory_region_tb_read(int cpu_index, uint64_t addr, uint64_t value, unsigned size) "cpu %d addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
95
-memory_region_tb_write(int cpu_index, uint64_t addr, uint64_t value, unsigned size) "cpu %d addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
96
memory_region_ram_device_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
97
memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
98
flatview_new(void *view, void *root) "%p (root %p)"
99
--
141
--
100
2.17.1
142
2.34.1
101
102
diff view generated by jsdifflib