2 * ARM v8.5-MemTag Operations
4 * Copyright (c) 2020 Linaro, Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
23 #include "internals.h"
24 #include "exec/exec-all.h"
25 #include "exec/ram_addr.h"
26 #include "exec/cpu_ldst.h"
27 #include "exec/helper-proto.h"
28 #include "qapi/error.h"
29 #include "qemu/guest-random.h"
32 static int choose_nonexcluded_tag(int tag, int offset, uint16_t exclude)
34 if (exclude == 0xffff) {
38 while (exclude & (1 << tag)) {
45 } while (exclude & (1 << tag));
46 } while (--offset > 0);
53 * @env: the cpu environment
54 * @ptr_mmu_idx: the addressing regime to use for the virtual address
55 * @ptr: the virtual address for which to look up tag memory
56 * @ptr_access: the access to use for the virtual address
57 * @ptr_size: the number of bytes in the normal memory access
58 * @tag_access: the access to use for the tag memory
59 * @tag_size: the number of bytes in the tag memory access
60 * @ra: the return address for exception handling
62 * Our tag memory is formatted as a sequence of little-endian nibbles.
63 * That is, the byte at (addr >> (LOG2_TAG_GRANULE + 1)) contains two
64 * tags, with the tag at [3:0] for the lower addr and the tag at [7:4]
65 * for the higher addr.
67 * Here, resolve the physical address from the virtual address, and return
68 * a pointer to the corresponding tag byte. Exit with exception if the
69 * virtual address is not accessible for @ptr_access.
71 * The @ptr_size and @tag_size values may not have an obvious relation
72 * due to the alignment of @ptr, and the number of tag checks required.
74 * If there is no tag storage corresponding to @ptr, return NULL.
76 static uint8_t *allocation_tag_mem(CPUARMState *env, int ptr_mmu_idx,
77 uint64_t ptr, MMUAccessType ptr_access,
78 int ptr_size, MMUAccessType tag_access,
79 int tag_size, uintptr_t ra)
81 #ifdef CONFIG_USER_ONLY
82 uint64_t clean_ptr = useronly_clean_ptr(ptr);
83 int flags = page_get_flags(clean_ptr);
87 if (!(flags & (ptr_access == MMU_DATA_STORE ? PAGE_WRITE_ORG : PAGE_READ))) {
88 cpu_loop_exit_sigsegv(env_cpu(env), ptr, ptr_access,
89 !(flags & PAGE_VALID), ra);
92 /* Require both MAP_ANON and PROT_MTE for the page. */
93 if (!(flags & PAGE_ANON) || !(flags & PAGE_MTE)) {
97 tags = page_get_target_data(clean_ptr);
99 index = extract32(ptr, LOG2_TAG_GRANULE + 1,
100 TARGET_PAGE_BITS - LOG2_TAG_GRANULE - 1);
103 CPUTLBEntryFull *full;
106 hwaddr ptr_paddr, tag_paddr, xlat;
109 AddressSpace *tag_as;
113 * Probe the first byte of the virtual address. This raises an
114 * exception for inaccessible pages, and resolves the virtual address
115 * into the softmmu tlb.
117 * When RA == 0, this is for mte_probe. The page is expected to be
118 * valid. Indicate to probe_access_flags no-fault, then assert that
119 * we received a valid page.
121 flags = probe_access_full(env, ptr, ptr_access, ptr_mmu_idx,
122 ra == 0, &host, &full, ra);
123 assert(!(flags & TLB_INVALID_MASK));
125 /* If the virtual page MemAttr != Tagged, access unchecked. */
126 if (full->pte_attrs != 0xf0) {
131 * If not backed by host ram, there is no tag storage: access unchecked.
132 * This is probably a guest os bug though, so log it.
134 if (unlikely(flags & TLB_MMIO)) {
135 qemu_log_mask(LOG_GUEST_ERROR,
136 "Page @ 0x%" PRIx64 " indicates Tagged Normal memory "
137 "but is not backed by host ram\n", ptr);
142 * Remember these values across the second lookup below,
143 * which may invalidate this pointer via tlb resize.
145 ptr_paddr = full->phys_addr;
150 * The Normal memory access can extend to the next page. E.g. a single
151 * 8-byte access to the last byte of a page will check only the last
152 * tag on the first page.
153 * Any page access exception has priority over tag check exception.
155 in_page = -(ptr | TARGET_PAGE_MASK);
156 if (unlikely(ptr_size > in_page)) {
157 flags |= probe_access_full(env, ptr + in_page, ptr_access,
158 ptr_mmu_idx, ra == 0, &host, &full, ra);
159 assert(!(flags & TLB_INVALID_MASK));
162 /* Any debug exception has priority over a tag check exception. */
163 if (unlikely(flags & TLB_WATCHPOINT)) {
164 int wp = ptr_access == MMU_DATA_LOAD ? BP_MEM_READ : BP_MEM_WRITE;
166 cpu_check_watchpoint(env_cpu(env), ptr, ptr_size, attrs, wp, ra);
169 /* Convert to the physical address in tag space. */
170 tag_paddr = ptr_paddr >> (LOG2_TAG_GRANULE + 1);
172 /* Look up the address in tag space. */
173 tag_asi = attrs.secure ? ARMASIdx_TagS : ARMASIdx_TagNS;
174 tag_as = cpu_get_address_space(env_cpu(env), tag_asi);
175 mr = address_space_translate(tag_as, tag_paddr, &xlat, NULL,
176 tag_access == MMU_DATA_STORE, attrs);
179 * Note that @mr will never be NULL. If there is nothing in the address
180 * space at @tag_paddr, the translation will return the unallocated memory
181 * region. For our purposes, the result must be ram.
183 if (unlikely(!memory_region_is_ram(mr))) {
184 /* ??? Failure is a board configuration error. */
185 qemu_log_mask(LOG_UNIMP,
186 "Tag Memory @ 0x%" HWADDR_PRIx " not found for "
187 "Normal Memory @ 0x%" HWADDR_PRIx "\n",
188 tag_paddr, ptr_paddr);
193 * Ensure the tag memory is dirty on write, for migration.
194 * Tag memory can never contain code or display memory (vga).
196 if (tag_access == MMU_DATA_STORE) {
197 ram_addr_t tag_ra = memory_region_get_ram_addr(mr) + xlat;
198 cpu_physical_memory_set_dirty_flag(tag_ra, DIRTY_MEMORY_MIGRATION);
201 return memory_region_get_ram_ptr(mr) + xlat;
205 uint64_t HELPER(irg)(CPUARMState *env, uint64_t rn, uint64_t rm)
207 uint16_t exclude = extract32(rm | env->cp15.gcr_el1, 0, 16);
208 int rrnd = extract32(env->cp15.gcr_el1, 16, 1);
209 int start = extract32(env->cp15.rgsr_el1, 0, 4);
210 int seed = extract32(env->cp15.rgsr_el1, 8, 16);
214 * Our IMPDEF choice for GCR_EL1.RRND==1 is to continue to use the
215 * deterministic algorithm. Except that with RRND==1 the kernel is
216 * not required to have set RGSR_EL1.SEED != 0, which is required for
217 * the deterministic algorithm to function. So we force a non-zero
218 * SEED for that case.
220 if (unlikely(seed == 0) && rrnd) {
225 if (qemu_guest_getrandom(&two, sizeof(two), &err) < 0) {
227 * Failed, for unknown reasons in the crypto subsystem.
228 * Best we can do is log the reason and use a constant seed.
230 qemu_log_mask(LOG_UNIMP, "IRG: Crypto failure: %s\n",
231 error_get_pretty(err));
240 for (i = offset = 0; i < 4; ++i) {
241 /* NextRandomTagBit */
242 int top = (extract32(seed, 5, 1) ^ extract32(seed, 3, 1) ^
243 extract32(seed, 2, 1) ^ extract32(seed, 0, 1));
244 seed = (top << 15) | (seed >> 1);
247 rtag = choose_nonexcluded_tag(start, offset, exclude);
248 env->cp15.rgsr_el1 = rtag | (seed << 8);
250 return address_with_allocation_tag(rn, rtag);
253 uint64_t HELPER(addsubg)(CPUARMState *env, uint64_t ptr,
254 int32_t offset, uint32_t tag_offset)
256 int start_tag = allocation_tag_from_addr(ptr);
257 uint16_t exclude = extract32(env->cp15.gcr_el1, 0, 16);
258 int rtag = choose_nonexcluded_tag(start_tag, tag_offset, exclude);
260 return address_with_allocation_tag(ptr + offset, rtag);
263 static int load_tag1(uint64_t ptr, uint8_t *mem)
265 int ofs = extract32(ptr, LOG2_TAG_GRANULE, 1) * 4;
266 return extract32(*mem, ofs, 4);
269 uint64_t HELPER(ldg)(CPUARMState *env, uint64_t ptr, uint64_t xt)
271 int mmu_idx = cpu_mmu_index(env, false);
275 /* Trap if accessing an invalid page. */
276 mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_LOAD, 1,
277 MMU_DATA_LOAD, 1, GETPC());
279 /* Load if page supports tags. */
281 rtag = load_tag1(ptr, mem);
284 return address_with_allocation_tag(xt, rtag);
287 static void check_tag_aligned(CPUARMState *env, uint64_t ptr, uintptr_t ra)
289 if (unlikely(!QEMU_IS_ALIGNED(ptr, TAG_GRANULE))) {
290 arm_cpu_do_unaligned_access(env_cpu(env), ptr, MMU_DATA_STORE,
291 cpu_mmu_index(env, false), ra);
292 g_assert_not_reached();
296 /* For use in a non-parallel context, store to the given nibble. */
297 static void store_tag1(uint64_t ptr, uint8_t *mem, int tag)
299 int ofs = extract32(ptr, LOG2_TAG_GRANULE, 1) * 4;
300 *mem = deposit32(*mem, ofs, 4, tag);
303 /* For use in a parallel context, atomically store to the given nibble. */
304 static void store_tag1_parallel(uint64_t ptr, uint8_t *mem, int tag)
306 int ofs = extract32(ptr, LOG2_TAG_GRANULE, 1) * 4;
307 uint8_t old = qatomic_read(mem);
310 uint8_t new = deposit32(old, ofs, 4, tag);
311 uint8_t cmp = qatomic_cmpxchg(mem, old, new);
312 if (likely(cmp == old)) {
319 typedef void stg_store1(uint64_t, uint8_t *, int);
321 static inline void do_stg(CPUARMState *env, uint64_t ptr, uint64_t xt,
322 uintptr_t ra, stg_store1 store1)
324 int mmu_idx = cpu_mmu_index(env, false);
327 check_tag_aligned(env, ptr, ra);
329 /* Trap if accessing an invalid page. */
330 mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE, TAG_GRANULE,
331 MMU_DATA_STORE, 1, ra);
333 /* Store if page supports tags. */
335 store1(ptr, mem, allocation_tag_from_addr(xt));
339 void HELPER(stg)(CPUARMState *env, uint64_t ptr, uint64_t xt)
341 do_stg(env, ptr, xt, GETPC(), store_tag1);
344 void HELPER(stg_parallel)(CPUARMState *env, uint64_t ptr, uint64_t xt)
346 do_stg(env, ptr, xt, GETPC(), store_tag1_parallel);
349 void HELPER(stg_stub)(CPUARMState *env, uint64_t ptr)
351 int mmu_idx = cpu_mmu_index(env, false);
352 uintptr_t ra = GETPC();
354 check_tag_aligned(env, ptr, ra);
355 probe_write(env, ptr, TAG_GRANULE, mmu_idx, ra);
358 static inline void do_st2g(CPUARMState *env, uint64_t ptr, uint64_t xt,
359 uintptr_t ra, stg_store1 store1)
361 int mmu_idx = cpu_mmu_index(env, false);
362 int tag = allocation_tag_from_addr(xt);
363 uint8_t *mem1, *mem2;
365 check_tag_aligned(env, ptr, ra);
368 * Trap if accessing an invalid page(s).
369 * This takes priority over !allocation_tag_access_enabled.
371 if (ptr & TAG_GRANULE) {
372 /* Two stores unaligned mod TAG_GRANULE*2 -- modify two bytes. */
373 mem1 = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE,
374 TAG_GRANULE, MMU_DATA_STORE, 1, ra);
375 mem2 = allocation_tag_mem(env, mmu_idx, ptr + TAG_GRANULE,
376 MMU_DATA_STORE, TAG_GRANULE,
377 MMU_DATA_STORE, 1, ra);
379 /* Store if page(s) support tags. */
381 store1(TAG_GRANULE, mem1, tag);
384 store1(0, mem2, tag);
387 /* Two stores aligned mod TAG_GRANULE*2 -- modify one byte. */
388 mem1 = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE,
389 2 * TAG_GRANULE, MMU_DATA_STORE, 1, ra);
392 qatomic_set(mem1, tag);
397 void HELPER(st2g)(CPUARMState *env, uint64_t ptr, uint64_t xt)
399 do_st2g(env, ptr, xt, GETPC(), store_tag1);
402 void HELPER(st2g_parallel)(CPUARMState *env, uint64_t ptr, uint64_t xt)
404 do_st2g(env, ptr, xt, GETPC(), store_tag1_parallel);
407 void HELPER(st2g_stub)(CPUARMState *env, uint64_t ptr)
409 int mmu_idx = cpu_mmu_index(env, false);
410 uintptr_t ra = GETPC();
411 int in_page = -(ptr | TARGET_PAGE_MASK);
413 check_tag_aligned(env, ptr, ra);
415 if (likely(in_page >= 2 * TAG_GRANULE)) {
416 probe_write(env, ptr, 2 * TAG_GRANULE, mmu_idx, ra);
418 probe_write(env, ptr, TAG_GRANULE, mmu_idx, ra);
419 probe_write(env, ptr + TAG_GRANULE, TAG_GRANULE, mmu_idx, ra);
423 #define LDGM_STGM_SIZE (4 << GMID_EL1_BS)
425 uint64_t HELPER(ldgm)(CPUARMState *env, uint64_t ptr)
427 int mmu_idx = cpu_mmu_index(env, false);
428 uintptr_t ra = GETPC();
431 ptr = QEMU_ALIGN_DOWN(ptr, LDGM_STGM_SIZE);
433 /* Trap if accessing an invalid page. */
434 tag_mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_LOAD,
435 LDGM_STGM_SIZE, MMU_DATA_LOAD,
436 LDGM_STGM_SIZE / (2 * TAG_GRANULE), ra);
438 /* The tag is squashed to zero if the page does not support tags. */
443 QEMU_BUILD_BUG_ON(GMID_EL1_BS != 6);
445 * We are loading 64-bits worth of tags. The ordering of elements
446 * within the word corresponds to a 64-bit little-endian operation.
448 return ldq_le_p(tag_mem);
451 void HELPER(stgm)(CPUARMState *env, uint64_t ptr, uint64_t val)
453 int mmu_idx = cpu_mmu_index(env, false);
454 uintptr_t ra = GETPC();
457 ptr = QEMU_ALIGN_DOWN(ptr, LDGM_STGM_SIZE);
459 /* Trap if accessing an invalid page. */
460 tag_mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE,
461 LDGM_STGM_SIZE, MMU_DATA_LOAD,
462 LDGM_STGM_SIZE / (2 * TAG_GRANULE), ra);
465 * Tag store only happens if the page support tags,
466 * and if the OS has enabled access to the tags.
472 QEMU_BUILD_BUG_ON(GMID_EL1_BS != 6);
474 * We are storing 64-bits worth of tags. The ordering of elements
475 * within the word corresponds to a 64-bit little-endian operation.
477 stq_le_p(tag_mem, val);
480 void HELPER(stzgm_tags)(CPUARMState *env, uint64_t ptr, uint64_t val)
482 uintptr_t ra = GETPC();
483 int mmu_idx = cpu_mmu_index(env, false);
484 int log2_dcz_bytes, log2_tag_bytes;
485 intptr_t dcz_bytes, tag_bytes;
489 * In arm_cpu_realizefn, we assert that dcz > LOG2_TAG_GRANULE+1,
490 * i.e. 32 bytes, which is an unreasonably small dcz anyway,
491 * to make sure that we can access one complete tag byte here.
493 log2_dcz_bytes = env_archcpu(env)->dcz_blocksize + 2;
494 log2_tag_bytes = log2_dcz_bytes - (LOG2_TAG_GRANULE + 1);
495 dcz_bytes = (intptr_t)1 << log2_dcz_bytes;
496 tag_bytes = (intptr_t)1 << log2_tag_bytes;
499 mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE, dcz_bytes,
500 MMU_DATA_STORE, tag_bytes, ra);
502 int tag_pair = (val & 0xf) * 0x11;
503 memset(mem, tag_pair, tag_bytes);
507 static void mte_sync_check_fail(CPUARMState *env, uint32_t desc,
508 uint64_t dirty_ptr, uintptr_t ra)
512 env->exception.vaddress = dirty_ptr;
514 is_write = FIELD_EX32(desc, MTEDESC, WRITE);
515 syn = syn_data_abort_no_iss(arm_current_el(env) != 0, 0, 0, 0, 0, is_write,
517 raise_exception_ra(env, EXCP_DATA_ABORT, syn, exception_target_el(env), ra);
518 g_assert_not_reached();
521 static void mte_async_check_fail(CPUARMState *env, uint64_t dirty_ptr,
522 uintptr_t ra, ARMMMUIdx arm_mmu_idx, int el)
526 if (regime_has_2_ranges(arm_mmu_idx)) {
527 select = extract64(dirty_ptr, 55, 1);
531 env->cp15.tfsr_el[el] |= 1 << select;
532 #ifdef CONFIG_USER_ONLY
534 * Stand in for a timer irq, setting _TIF_MTE_ASYNC_FAULT,
535 * which then sends a SIGSEGV when the thread is next scheduled.
536 * This cpu will return to the main loop at the end of the TB,
537 * which is rather sooner than "normal". But the alternative
538 * is waiting until the next syscall.
540 qemu_cpu_kick(env_cpu(env));
544 /* Record a tag check failure. */
545 static void mte_check_fail(CPUARMState *env, uint32_t desc,
546 uint64_t dirty_ptr, uintptr_t ra)
548 int mmu_idx = FIELD_EX32(desc, MTEDESC, MIDX);
549 ARMMMUIdx arm_mmu_idx = core_to_aa64_mmu_idx(mmu_idx);
553 reg_el = regime_el(env, arm_mmu_idx);
554 sctlr = env->cp15.sctlr_el[reg_el];
556 switch (arm_mmu_idx) {
557 case ARMMMUIdx_E10_0:
558 case ARMMMUIdx_E20_0:
560 tcf = extract64(sctlr, 38, 2);
564 tcf = extract64(sctlr, 40, 2);
569 /* Tag check fail causes a synchronous exception. */
570 mte_sync_check_fail(env, desc, dirty_ptr, ra);
575 * Tag check fail does not affect the PE.
576 * We eliminate this case by not setting MTE_ACTIVE
577 * in tb_flags, so that we never make this runtime call.
579 g_assert_not_reached();
582 /* Tag check fail causes asynchronous flag set. */
583 mte_async_check_fail(env, dirty_ptr, ra, arm_mmu_idx, el);
588 * Tag check fail causes asynchronous flag set for stores, or
589 * a synchronous exception for loads.
591 if (FIELD_EX32(desc, MTEDESC, WRITE)) {
592 mte_async_check_fail(env, dirty_ptr, ra, arm_mmu_idx, el);
594 mte_sync_check_fail(env, desc, dirty_ptr, ra);
602 * @tag: tag memory to test
603 * @odd: true to begin testing at tags at odd nibble
604 * @cmp: the tag to compare against
605 * @count: number of tags to test
607 * Return the number of successful tests.
608 * Thus a return value < @count indicates a failure.
610 * A note about sizes: count is expected to be small.
612 * The most common use will be LDP/STP of two integer registers,
613 * which means 16 bytes of memory touching at most 2 tags, but
614 * often the access is aligned and thus just 1 tag.
616 * Using AdvSIMD LD/ST (multiple), one can access 64 bytes of memory,
617 * touching at most 5 tags. SVE LDR/STR (vector) with the default
618 * vector length is also 64 bytes; the maximum architectural length
619 * is 256 bytes touching at most 9 tags.
621 * The loop below uses 7 logical operations and 1 memory operation
622 * per tag pair. An implementation that loads an aligned word and
623 * uses masking to ignore adjacent tags requires 18 logical operations
624 * and thus does not begin to pay off until 6 tags.
625 * Which, according to the survey above, is unlikely to be common.
627 static int checkN(uint8_t *mem, int odd, int cmp, int count)
631 /* Replicate the test tag and compare. */
641 if (unlikely((diff) & 0x0f)) {
650 if (unlikely((diff) & 0xf0)) {
663 * mte_probe_int() - helper for mte_probe and mte_check
664 * @env: CPU environment
665 * @desc: MTEDESC descriptor
666 * @ptr: virtual address of the base of the access
667 * @fault: return virtual address of the first check failure
669 * Internal routine for both mte_probe and mte_check.
670 * Return zero on failure, filling in *fault.
671 * Return negative on trivial success for tbi disabled.
672 * Return positive on success with tbi enabled.
674 static int mte_probe_int(CPUARMState *env, uint32_t desc, uint64_t ptr,
675 uintptr_t ra, uint64_t *fault)
677 int mmu_idx, ptr_tag, bit55;
678 uint64_t ptr_last, prev_page, next_page;
679 uint64_t tag_first, tag_last;
680 uint64_t tag_byte_first, tag_byte_last;
681 uint32_t sizem1, tag_count, tag_size, n, c;
682 uint8_t *mem1, *mem2;
685 bit55 = extract64(ptr, 55, 1);
688 /* If TBI is disabled, the access is unchecked, and ptr is not dirty. */
689 if (unlikely(!tbi_check(desc, bit55))) {
693 ptr_tag = allocation_tag_from_addr(ptr);
695 if (tcma_check(desc, bit55, ptr_tag)) {
699 mmu_idx = FIELD_EX32(desc, MTEDESC, MIDX);
700 type = FIELD_EX32(desc, MTEDESC, WRITE) ? MMU_DATA_STORE : MMU_DATA_LOAD;
701 sizem1 = FIELD_EX32(desc, MTEDESC, SIZEM1);
703 /* Find the addr of the end of the access */
704 ptr_last = ptr + sizem1;
706 /* Round the bounds to the tag granule, and compute the number of tags. */
707 tag_first = QEMU_ALIGN_DOWN(ptr, TAG_GRANULE);
708 tag_last = QEMU_ALIGN_DOWN(ptr_last, TAG_GRANULE);
709 tag_count = ((tag_last - tag_first) / TAG_GRANULE) + 1;
711 /* Round the bounds to twice the tag granule, and compute the bytes. */
712 tag_byte_first = QEMU_ALIGN_DOWN(ptr, 2 * TAG_GRANULE);
713 tag_byte_last = QEMU_ALIGN_DOWN(ptr_last, 2 * TAG_GRANULE);
715 /* Locate the page boundaries. */
716 prev_page = ptr & TARGET_PAGE_MASK;
717 next_page = prev_page + TARGET_PAGE_SIZE;
719 if (likely(tag_last - prev_page < TARGET_PAGE_SIZE)) {
720 /* Memory access stays on one page. */
721 tag_size = ((tag_byte_last - tag_byte_first) / (2 * TAG_GRANULE)) + 1;
722 mem1 = allocation_tag_mem(env, mmu_idx, ptr, type, sizem1 + 1,
723 MMU_DATA_LOAD, tag_size, ra);
727 /* Perform all of the comparisons. */
728 n = checkN(mem1, ptr & TAG_GRANULE, ptr_tag, tag_count);
730 /* Memory access crosses to next page. */
731 tag_size = (next_page - tag_byte_first) / (2 * TAG_GRANULE);
732 mem1 = allocation_tag_mem(env, mmu_idx, ptr, type, next_page - ptr,
733 MMU_DATA_LOAD, tag_size, ra);
735 tag_size = ((tag_byte_last - next_page) / (2 * TAG_GRANULE)) + 1;
736 mem2 = allocation_tag_mem(env, mmu_idx, next_page, type,
737 ptr_last - next_page + 1,
738 MMU_DATA_LOAD, tag_size, ra);
741 * Perform all of the comparisons.
742 * Note the possible but unlikely case of the operation spanning
743 * two pages that do not both have tagging enabled.
745 n = c = (next_page - tag_first) / TAG_GRANULE;
747 n = checkN(mem1, ptr & TAG_GRANULE, ptr_tag, c);
753 n += checkN(mem2, 0, ptr_tag, tag_count - c);
757 if (likely(n == tag_count)) {
762 * If we failed, we know which granule. For the first granule, the
763 * failure address is @ptr, the first byte accessed. Otherwise the
764 * failure address is the first byte of the nth granule.
767 *fault = tag_first + n * TAG_GRANULE;
772 uint64_t mte_check(CPUARMState *env, uint32_t desc, uint64_t ptr, uintptr_t ra)
775 int ret = mte_probe_int(env, desc, ptr, ra, &fault);
777 if (unlikely(ret == 0)) {
778 mte_check_fail(env, desc, fault, ra);
779 } else if (ret < 0) {
782 return useronly_clean_ptr(ptr);
785 uint64_t HELPER(mte_check)(CPUARMState *env, uint32_t desc, uint64_t ptr)
787 return mte_check(env, desc, ptr, GETPC());
791 * No-fault version of mte_check, to be used by SVE for MemSingleNF.
792 * Returns false if the access is Checked and the check failed. This
793 * is only intended to probe the tag -- the validity of the page must
794 * be checked beforehand.
796 bool mte_probe(CPUARMState *env, uint32_t desc, uint64_t ptr)
799 int ret = mte_probe_int(env, desc, ptr, 0, &fault);
805 * Perform an MTE checked access for DC_ZVA.
807 uint64_t HELPER(mte_check_zva)(CPUARMState *env, uint32_t desc, uint64_t ptr)
809 uintptr_t ra = GETPC();
810 int log2_dcz_bytes, log2_tag_bytes;
812 intptr_t dcz_bytes, tag_bytes, i;
814 uint64_t ptr_tag, mem_tag, align_ptr;
816 bit55 = extract64(ptr, 55, 1);
818 /* If TBI is disabled, the access is unchecked, and ptr is not dirty. */
819 if (unlikely(!tbi_check(desc, bit55))) {
823 ptr_tag = allocation_tag_from_addr(ptr);
825 if (tcma_check(desc, bit55, ptr_tag)) {
830 * In arm_cpu_realizefn, we asserted that dcz > LOG2_TAG_GRANULE+1,
831 * i.e. 32 bytes, which is an unreasonably small dcz anyway, to make
832 * sure that we can access one complete tag byte here.
834 log2_dcz_bytes = env_archcpu(env)->dcz_blocksize + 2;
835 log2_tag_bytes = log2_dcz_bytes - (LOG2_TAG_GRANULE + 1);
836 dcz_bytes = (intptr_t)1 << log2_dcz_bytes;
837 tag_bytes = (intptr_t)1 << log2_tag_bytes;
838 align_ptr = ptr & -dcz_bytes;
841 * Trap if accessing an invalid page. DC_ZVA requires that we supply
842 * the original pointer for an invalid page. But watchpoints require
843 * that we probe the actual space. So do both.
845 mmu_idx = FIELD_EX32(desc, MTEDESC, MIDX);
846 (void) probe_write(env, ptr, 1, mmu_idx, ra);
847 mem = allocation_tag_mem(env, mmu_idx, align_ptr, MMU_DATA_STORE,
848 dcz_bytes, MMU_DATA_LOAD, tag_bytes, ra);
854 * Unlike the reasoning for checkN, DC_ZVA is always aligned, and thus
855 * it is quite easy to perform all of the comparisons at once without
858 * The most common zva block size is 64; some of the thunderx cpus use
859 * a block size of 128. For user-only, aarch64_max_initfn will set the
860 * block size to 512. Fill out the other cases for future-proofing.
862 * In order to be able to find the first miscompare later, we want the
863 * tag bytes to be in little-endian order.
865 switch (log2_tag_bytes) {
866 case 0: /* zva_blocksize 32 */
867 mem_tag = *(uint8_t *)mem;
870 case 1: /* zva_blocksize 64 */
871 mem_tag = cpu_to_le16(*(uint16_t *)mem);
874 case 2: /* zva_blocksize 128 */
875 mem_tag = cpu_to_le32(*(uint32_t *)mem);
876 ptr_tag *= 0x11111111u;
878 case 3: /* zva_blocksize 256 */
879 mem_tag = cpu_to_le64(*(uint64_t *)mem);
880 ptr_tag *= 0x1111111111111111ull;
883 default: /* zva_blocksize 512, 1024, 2048 */
884 ptr_tag *= 0x1111111111111111ull;
887 mem_tag = cpu_to_le64(*(uint64_t *)(mem + i));
888 if (unlikely(mem_tag != ptr_tag)) {
892 align_ptr += 16 * TAG_GRANULE;
893 } while (i < tag_bytes);
897 if (likely(mem_tag == ptr_tag)) {
902 /* Locate the first nibble that differs. */
903 i = ctz64(mem_tag ^ ptr_tag) >> 4;
904 mte_check_fail(env, desc, align_ptr + i * TAG_GRANULE, ra);
907 return useronly_clean_ptr(ptr);