accel/tcg: Return bool from page_check_range

Replace the 0/-1 result with true/false.
Invert the sense of the test of all callers.
Document the function.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20230707204054.8792-25-richard.henderson@linaro.org>
master
Richard Henderson 2023-07-07 21:40:52 +01:00
parent 91e9e116fe
commit bef6f008b9
9 changed files with 31 additions and 20 deletions

View File

@ -159,7 +159,7 @@ static uint64_t load_atomic8_or_exit(CPUArchState *env, uintptr_t ra, void *pv)
* another process, because the fallback start_exclusive solution * another process, because the fallback start_exclusive solution
* provides no protection across processes. * provides no protection across processes.
*/ */
if (!page_check_range(h2g(pv), 8, PAGE_WRITE_ORG)) { if (page_check_range(h2g(pv), 8, PAGE_WRITE_ORG)) {
uint64_t *p = __builtin_assume_aligned(pv, 8); uint64_t *p = __builtin_assume_aligned(pv, 8);
return *p; return *p;
} }
@ -194,7 +194,7 @@ static Int128 load_atomic16_or_exit(CPUArchState *env, uintptr_t ra, void *pv)
* another process, because the fallback start_exclusive solution * another process, because the fallback start_exclusive solution
* provides no protection across processes. * provides no protection across processes.
*/ */
if (!page_check_range(h2g(p), 16, PAGE_WRITE_ORG)) { if (page_check_range(h2g(p), 16, PAGE_WRITE_ORG)) {
return *p; return *p;
} }
#endif #endif

View File

@ -520,19 +520,19 @@ void page_set_flags(target_ulong start, target_ulong last, int flags)
} }
} }
int page_check_range(target_ulong start, target_ulong len, int flags) bool page_check_range(target_ulong start, target_ulong len, int flags)
{ {
target_ulong last; target_ulong last;
int locked; /* tri-state: =0: unlocked, +1: global, -1: local */ int locked; /* tri-state: =0: unlocked, +1: global, -1: local */
int ret; bool ret;
if (len == 0) { if (len == 0) {
return 0; /* trivial length */ return true; /* trivial length */
} }
last = start + len - 1; last = start + len - 1;
if (last < start) { if (last < start) {
return -1; /* wrap around */ return false; /* wrap around */
} }
locked = have_mmap_lock(); locked = have_mmap_lock();
@ -551,33 +551,33 @@ int page_check_range(target_ulong start, target_ulong len, int flags)
p = pageflags_find(start, last); p = pageflags_find(start, last);
} }
if (!p) { if (!p) {
ret = -1; /* entire region invalid */ ret = false; /* entire region invalid */
break; break;
} }
} }
if (start < p->itree.start) { if (start < p->itree.start) {
ret = -1; /* initial bytes invalid */ ret = false; /* initial bytes invalid */
break; break;
} }
missing = flags & ~p->flags; missing = flags & ~p->flags;
if (missing & ~PAGE_WRITE) { if (missing & ~PAGE_WRITE) {
ret = -1; /* page doesn't match */ ret = false; /* page doesn't match */
break; break;
} }
if (missing & PAGE_WRITE) { if (missing & PAGE_WRITE) {
if (!(p->flags & PAGE_WRITE_ORG)) { if (!(p->flags & PAGE_WRITE_ORG)) {
ret = -1; /* page not writable */ ret = false; /* page not writable */
break; break;
} }
/* Asking about writable, but has been protected: undo. */ /* Asking about writable, but has been protected: undo. */
if (!page_unprotect(start, 0)) { if (!page_unprotect(start, 0)) {
ret = -1; ret = false;
break; break;
} }
/* TODO: page_unprotect should take a range, not a single page. */ /* TODO: page_unprotect should take a range, not a single page. */
if (last - start < TARGET_PAGE_SIZE) { if (last - start < TARGET_PAGE_SIZE) {
ret = 0; /* ok */ ret = true; /* ok */
break; break;
} }
start += TARGET_PAGE_SIZE; start += TARGET_PAGE_SIZE;
@ -585,7 +585,7 @@ int page_check_range(target_ulong start, target_ulong len, int flags)
} }
if (last <= p->itree.last) { if (last <= p->itree.last) {
ret = 0; /* ok */ ret = true; /* ok */
break; break;
} }
start = p->itree.last + 1; start = p->itree.last + 1;

View File

@ -267,7 +267,7 @@ abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
static inline bool access_ok(int type, abi_ulong addr, abi_ulong size) static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
{ {
return page_check_range((target_ulong)addr, size, type) == 0; return page_check_range((target_ulong)addr, size, type);
} }
/* /*

View File

@ -222,7 +222,18 @@ int walk_memory_regions(void *, walk_memory_regions_fn);
int page_get_flags(target_ulong address); int page_get_flags(target_ulong address);
void page_set_flags(target_ulong start, target_ulong last, int flags); void page_set_flags(target_ulong start, target_ulong last, int flags);
void page_reset_target_data(target_ulong start, target_ulong last); void page_reset_target_data(target_ulong start, target_ulong last);
int page_check_range(target_ulong start, target_ulong len, int flags);
/**
* page_check_range
* @start: first byte of range
* @len: length of range
* @flags: flags required for each page
*
* Return true if every page in [@start, @start+@len) has @flags set.
* Return false if any page is unmapped. Thus testing flags == 0 is
* equivalent to testing for flags == PAGE_VALID.
*/
bool page_check_range(target_ulong start, target_ulong last, int flags);
/** /**
* page_check_range_empty: * page_check_range_empty:

View File

@ -182,7 +182,7 @@ static inline bool access_ok_untagged(int type, abi_ulong addr, abi_ulong size)
: !guest_range_valid_untagged(addr, size)) { : !guest_range_valid_untagged(addr, size)) {
return false; return false;
} }
return page_check_range((target_ulong)addr, size, type) == 0; return page_check_range((target_ulong)addr, size, type);
} }
static inline bool access_ok(CPUState *cpu, int type, static inline bool access_ok(CPUState *cpu, int type,

View File

@ -8122,7 +8122,7 @@ static int open_self_maps_1(CPUArchState *cpu_env, int fd, bool smaps)
max = h2g_valid(max - 1) ? max = h2g_valid(max - 1) ?
max : (uintptr_t) g2h_untagged(GUEST_ADDR_MAX) + 1; max : (uintptr_t) g2h_untagged(GUEST_ADDR_MAX) + 1;
if (page_check_range(h2g(min), max - min, flags) == -1) { if (!page_check_range(h2g(min), max - min, flags)) {
continue; continue;
} }

View File

@ -168,7 +168,7 @@ target_ureg HELPER(probe)(CPUHPPAState *env, target_ulong addr,
uint32_t level, uint32_t want) uint32_t level, uint32_t want)
{ {
#ifdef CONFIG_USER_ONLY #ifdef CONFIG_USER_ONLY
return (page_check_range(addr, 1, want) == 0) ? 1 : 0; return page_check_range(addr, 1, want);
#else #else
int prot, excp; int prot, excp;
hwaddr phys; hwaddr phys;

View File

@ -583,7 +583,7 @@ vext_ldff(void *vd, void *v0, target_ulong base,
cpu_mmu_index(env, false)); cpu_mmu_index(env, false));
if (host) { if (host) {
#ifdef CONFIG_USER_ONLY #ifdef CONFIG_USER_ONLY
if (page_check_range(addr, offset, PAGE_READ) < 0) { if (page_check_range(addr, offset, PAGE_READ)) {
vl = i; vl = i;
goto ProbeSuccess; goto ProbeSuccess;
} }

View File

@ -1191,7 +1191,7 @@ uint64_t helper_ld_asi(CPUSPARCState *env, target_ulong addr,
case ASI_PNFL: /* Primary no-fault LE */ case ASI_PNFL: /* Primary no-fault LE */
case ASI_SNF: /* Secondary no-fault */ case ASI_SNF: /* Secondary no-fault */
case ASI_SNFL: /* Secondary no-fault LE */ case ASI_SNFL: /* Secondary no-fault LE */
if (page_check_range(addr, size, PAGE_READ) == -1) { if (!page_check_range(addr, size, PAGE_READ)) {
ret = 0; ret = 0;
break; break;
} }