linux-user: Fix member types of target_dirent64
[qemu/ar7.git] / target / arm / mte_helper.c
blobe09b7e46a2ff61e33dc6067cd463704de15fc7d3
1 /*
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"
21 #include "cpu.h"
22 #include "internals.h"
23 #include "exec/exec-all.h"
24 #include "exec/ram_addr.h"
25 #include "exec/cpu_ldst.h"
26 #include "exec/helper-proto.h"
27 #include "qapi/error.h"
28 #include "qemu/guest-random.h"
31 static int choose_nonexcluded_tag(int tag, int offset, uint16_t exclude)
33 if (exclude == 0xffff) {
34 return 0;
36 if (offset == 0) {
37 while (exclude & (1 << tag)) {
38 tag = (tag + 1) & 15;
40 } else {
41 do {
42 do {
43 tag = (tag + 1) & 15;
44 } while (exclude & (1 << tag));
45 } while (--offset > 0);
47 return tag;
50 /**
51 * allocation_tag_mem:
52 * @env: the cpu environment
53 * @ptr_mmu_idx: the addressing regime to use for the virtual address
54 * @ptr: the virtual address for which to look up tag memory
55 * @ptr_access: the access to use for the virtual address
56 * @ptr_size: the number of bytes in the normal memory access
57 * @tag_access: the access to use for the tag memory
58 * @tag_size: the number of bytes in the tag memory access
59 * @ra: the return address for exception handling
61 * Our tag memory is formatted as a sequence of little-endian nibbles.
62 * That is, the byte at (addr >> (LOG2_TAG_GRANULE + 1)) contains two
63 * tags, with the tag at [3:0] for the lower addr and the tag at [7:4]
64 * for the higher addr.
66 * Here, resolve the physical address from the virtual address, and return
67 * a pointer to the corresponding tag byte. Exit with exception if the
68 * virtual address is not accessible for @ptr_access.
70 * The @ptr_size and @tag_size values may not have an obvious relation
71 * due to the alignment of @ptr, and the number of tag checks required.
73 * If there is no tag storage corresponding to @ptr, return NULL.
75 static uint8_t *allocation_tag_mem(CPUARMState *env, int ptr_mmu_idx,
76 uint64_t ptr, MMUAccessType ptr_access,
77 int ptr_size, MMUAccessType tag_access,
78 int tag_size, uintptr_t ra)
80 #ifdef CONFIG_USER_ONLY
81 uint64_t clean_ptr = useronly_clean_ptr(ptr);
82 int flags = page_get_flags(clean_ptr);
83 uint8_t *tags;
84 uintptr_t index;
86 if (!(flags & (ptr_access == MMU_DATA_STORE ? PAGE_WRITE_ORG : PAGE_READ))) {
87 cpu_loop_exit_sigsegv(env_cpu(env), ptr, ptr_access,
88 !(flags & PAGE_VALID), ra);
91 /* Require both MAP_ANON and PROT_MTE for the page. */
92 if (!(flags & PAGE_ANON) || !(flags & PAGE_MTE)) {
93 return NULL;
96 tags = page_get_target_data(clean_ptr);
97 if (tags == NULL) {
98 size_t alloc_size = TARGET_PAGE_SIZE >> (LOG2_TAG_GRANULE + 1);
99 tags = page_alloc_target_data(clean_ptr, alloc_size);
100 assert(tags != NULL);
103 index = extract32(ptr, LOG2_TAG_GRANULE + 1,
104 TARGET_PAGE_BITS - LOG2_TAG_GRANULE - 1);
105 return tags + index;
106 #else
107 uintptr_t index;
108 CPUIOTLBEntry *iotlbentry;
109 int in_page, flags;
110 ram_addr_t ptr_ra;
111 hwaddr ptr_paddr, tag_paddr, xlat;
112 MemoryRegion *mr;
113 ARMASIdx tag_asi;
114 AddressSpace *tag_as;
115 void *host;
118 * Probe the first byte of the virtual address. This raises an
119 * exception for inaccessible pages, and resolves the virtual address
120 * into the softmmu tlb.
122 * When RA == 0, this is for mte_probe. The page is expected to be
123 * valid. Indicate to probe_access_flags no-fault, then assert that
124 * we received a valid page.
126 flags = probe_access_flags(env, ptr, ptr_access, ptr_mmu_idx,
127 ra == 0, &host, ra);
128 assert(!(flags & TLB_INVALID_MASK));
131 * Find the iotlbentry for ptr. This *must* be present in the TLB
132 * because we just found the mapping.
133 * TODO: Perhaps there should be a cputlb helper that returns a
134 * matching tlb entry + iotlb entry.
136 index = tlb_index(env, ptr_mmu_idx, ptr);
137 # ifdef CONFIG_DEBUG_TCG
139 CPUTLBEntry *entry = tlb_entry(env, ptr_mmu_idx, ptr);
140 target_ulong comparator = (ptr_access == MMU_DATA_LOAD
141 ? entry->addr_read
142 : tlb_addr_write(entry));
143 g_assert(tlb_hit(comparator, ptr));
145 # endif
146 iotlbentry = &env_tlb(env)->d[ptr_mmu_idx].iotlb[index];
148 /* If the virtual page MemAttr != Tagged, access unchecked. */
149 if (!arm_tlb_mte_tagged(&iotlbentry->attrs)) {
150 return NULL;
154 * If not backed by host ram, there is no tag storage: access unchecked.
155 * This is probably a guest os bug though, so log it.
157 if (unlikely(flags & TLB_MMIO)) {
158 qemu_log_mask(LOG_GUEST_ERROR,
159 "Page @ 0x%" PRIx64 " indicates Tagged Normal memory "
160 "but is not backed by host ram\n", ptr);
161 return NULL;
165 * The Normal memory access can extend to the next page. E.g. a single
166 * 8-byte access to the last byte of a page will check only the last
167 * tag on the first page.
168 * Any page access exception has priority over tag check exception.
170 in_page = -(ptr | TARGET_PAGE_MASK);
171 if (unlikely(ptr_size > in_page)) {
172 void *ignore;
173 flags |= probe_access_flags(env, ptr + in_page, ptr_access,
174 ptr_mmu_idx, ra == 0, &ignore, ra);
175 assert(!(flags & TLB_INVALID_MASK));
178 /* Any debug exception has priority over a tag check exception. */
179 if (unlikely(flags & TLB_WATCHPOINT)) {
180 int wp = ptr_access == MMU_DATA_LOAD ? BP_MEM_READ : BP_MEM_WRITE;
181 assert(ra != 0);
182 cpu_check_watchpoint(env_cpu(env), ptr, ptr_size,
183 iotlbentry->attrs, wp, ra);
187 * Find the physical address within the normal mem space.
188 * The memory region lookup must succeed because TLB_MMIO was
189 * not set in the cputlb lookup above.
191 mr = memory_region_from_host(host, &ptr_ra);
192 tcg_debug_assert(mr != NULL);
193 tcg_debug_assert(memory_region_is_ram(mr));
194 ptr_paddr = ptr_ra;
195 do {
196 ptr_paddr += mr->addr;
197 mr = mr->container;
198 } while (mr);
200 /* Convert to the physical address in tag space. */
201 tag_paddr = ptr_paddr >> (LOG2_TAG_GRANULE + 1);
203 /* Look up the address in tag space. */
204 tag_asi = iotlbentry->attrs.secure ? ARMASIdx_TagS : ARMASIdx_TagNS;
205 tag_as = cpu_get_address_space(env_cpu(env), tag_asi);
206 mr = address_space_translate(tag_as, tag_paddr, &xlat, NULL,
207 tag_access == MMU_DATA_STORE,
208 iotlbentry->attrs);
211 * Note that @mr will never be NULL. If there is nothing in the address
212 * space at @tag_paddr, the translation will return the unallocated memory
213 * region. For our purposes, the result must be ram.
215 if (unlikely(!memory_region_is_ram(mr))) {
216 /* ??? Failure is a board configuration error. */
217 qemu_log_mask(LOG_UNIMP,
218 "Tag Memory @ 0x%" HWADDR_PRIx " not found for "
219 "Normal Memory @ 0x%" HWADDR_PRIx "\n",
220 tag_paddr, ptr_paddr);
221 return NULL;
225 * Ensure the tag memory is dirty on write, for migration.
226 * Tag memory can never contain code or display memory (vga).
228 if (tag_access == MMU_DATA_STORE) {
229 ram_addr_t tag_ra = memory_region_get_ram_addr(mr) + xlat;
230 cpu_physical_memory_set_dirty_flag(tag_ra, DIRTY_MEMORY_MIGRATION);
233 return memory_region_get_ram_ptr(mr) + xlat;
234 #endif
237 uint64_t HELPER(irg)(CPUARMState *env, uint64_t rn, uint64_t rm)
239 uint16_t exclude = extract32(rm | env->cp15.gcr_el1, 0, 16);
240 int rrnd = extract32(env->cp15.gcr_el1, 16, 1);
241 int start = extract32(env->cp15.rgsr_el1, 0, 4);
242 int seed = extract32(env->cp15.rgsr_el1, 8, 16);
243 int offset, i, rtag;
246 * Our IMPDEF choice for GCR_EL1.RRND==1 is to continue to use the
247 * deterministic algorithm. Except that with RRND==1 the kernel is
248 * not required to have set RGSR_EL1.SEED != 0, which is required for
249 * the deterministic algorithm to function. So we force a non-zero
250 * SEED for that case.
252 if (unlikely(seed == 0) && rrnd) {
253 do {
254 Error *err = NULL;
255 uint16_t two;
257 if (qemu_guest_getrandom(&two, sizeof(two), &err) < 0) {
259 * Failed, for unknown reasons in the crypto subsystem.
260 * Best we can do is log the reason and use a constant seed.
262 qemu_log_mask(LOG_UNIMP, "IRG: Crypto failure: %s\n",
263 error_get_pretty(err));
264 error_free(err);
265 two = 1;
267 seed = two;
268 } while (seed == 0);
271 /* RandomTag */
272 for (i = offset = 0; i < 4; ++i) {
273 /* NextRandomTagBit */
274 int top = (extract32(seed, 5, 1) ^ extract32(seed, 3, 1) ^
275 extract32(seed, 2, 1) ^ extract32(seed, 0, 1));
276 seed = (top << 15) | (seed >> 1);
277 offset |= top << i;
279 rtag = choose_nonexcluded_tag(start, offset, exclude);
280 env->cp15.rgsr_el1 = rtag | (seed << 8);
282 return address_with_allocation_tag(rn, rtag);
285 uint64_t HELPER(addsubg)(CPUARMState *env, uint64_t ptr,
286 int32_t offset, uint32_t tag_offset)
288 int start_tag = allocation_tag_from_addr(ptr);
289 uint16_t exclude = extract32(env->cp15.gcr_el1, 0, 16);
290 int rtag = choose_nonexcluded_tag(start_tag, tag_offset, exclude);
292 return address_with_allocation_tag(ptr + offset, rtag);
295 static int load_tag1(uint64_t ptr, uint8_t *mem)
297 int ofs = extract32(ptr, LOG2_TAG_GRANULE, 1) * 4;
298 return extract32(*mem, ofs, 4);
301 uint64_t HELPER(ldg)(CPUARMState *env, uint64_t ptr, uint64_t xt)
303 int mmu_idx = cpu_mmu_index(env, false);
304 uint8_t *mem;
305 int rtag = 0;
307 /* Trap if accessing an invalid page. */
308 mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_LOAD, 1,
309 MMU_DATA_LOAD, 1, GETPC());
311 /* Load if page supports tags. */
312 if (mem) {
313 rtag = load_tag1(ptr, mem);
316 return address_with_allocation_tag(xt, rtag);
319 static void check_tag_aligned(CPUARMState *env, uint64_t ptr, uintptr_t ra)
321 if (unlikely(!QEMU_IS_ALIGNED(ptr, TAG_GRANULE))) {
322 arm_cpu_do_unaligned_access(env_cpu(env), ptr, MMU_DATA_STORE,
323 cpu_mmu_index(env, false), ra);
324 g_assert_not_reached();
328 /* For use in a non-parallel context, store to the given nibble. */
329 static void store_tag1(uint64_t ptr, uint8_t *mem, int tag)
331 int ofs = extract32(ptr, LOG2_TAG_GRANULE, 1) * 4;
332 *mem = deposit32(*mem, ofs, 4, tag);
335 /* For use in a parallel context, atomically store to the given nibble. */
336 static void store_tag1_parallel(uint64_t ptr, uint8_t *mem, int tag)
338 int ofs = extract32(ptr, LOG2_TAG_GRANULE, 1) * 4;
339 uint8_t old = qatomic_read(mem);
341 while (1) {
342 uint8_t new = deposit32(old, ofs, 4, tag);
343 uint8_t cmp = qatomic_cmpxchg(mem, old, new);
344 if (likely(cmp == old)) {
345 return;
347 old = cmp;
351 typedef void stg_store1(uint64_t, uint8_t *, int);
353 static inline void do_stg(CPUARMState *env, uint64_t ptr, uint64_t xt,
354 uintptr_t ra, stg_store1 store1)
356 int mmu_idx = cpu_mmu_index(env, false);
357 uint8_t *mem;
359 check_tag_aligned(env, ptr, ra);
361 /* Trap if accessing an invalid page. */
362 mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE, TAG_GRANULE,
363 MMU_DATA_STORE, 1, ra);
365 /* Store if page supports tags. */
366 if (mem) {
367 store1(ptr, mem, allocation_tag_from_addr(xt));
371 void HELPER(stg)(CPUARMState *env, uint64_t ptr, uint64_t xt)
373 do_stg(env, ptr, xt, GETPC(), store_tag1);
376 void HELPER(stg_parallel)(CPUARMState *env, uint64_t ptr, uint64_t xt)
378 do_stg(env, ptr, xt, GETPC(), store_tag1_parallel);
381 void HELPER(stg_stub)(CPUARMState *env, uint64_t ptr)
383 int mmu_idx = cpu_mmu_index(env, false);
384 uintptr_t ra = GETPC();
386 check_tag_aligned(env, ptr, ra);
387 probe_write(env, ptr, TAG_GRANULE, mmu_idx, ra);
390 static inline void do_st2g(CPUARMState *env, uint64_t ptr, uint64_t xt,
391 uintptr_t ra, stg_store1 store1)
393 int mmu_idx = cpu_mmu_index(env, false);
394 int tag = allocation_tag_from_addr(xt);
395 uint8_t *mem1, *mem2;
397 check_tag_aligned(env, ptr, ra);
400 * Trap if accessing an invalid page(s).
401 * This takes priority over !allocation_tag_access_enabled.
403 if (ptr & TAG_GRANULE) {
404 /* Two stores unaligned mod TAG_GRANULE*2 -- modify two bytes. */
405 mem1 = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE,
406 TAG_GRANULE, MMU_DATA_STORE, 1, ra);
407 mem2 = allocation_tag_mem(env, mmu_idx, ptr + TAG_GRANULE,
408 MMU_DATA_STORE, TAG_GRANULE,
409 MMU_DATA_STORE, 1, ra);
411 /* Store if page(s) support tags. */
412 if (mem1) {
413 store1(TAG_GRANULE, mem1, tag);
415 if (mem2) {
416 store1(0, mem2, tag);
418 } else {
419 /* Two stores aligned mod TAG_GRANULE*2 -- modify one byte. */
420 mem1 = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE,
421 2 * TAG_GRANULE, MMU_DATA_STORE, 1, ra);
422 if (mem1) {
423 tag |= tag << 4;
424 qatomic_set(mem1, tag);
429 void HELPER(st2g)(CPUARMState *env, uint64_t ptr, uint64_t xt)
431 do_st2g(env, ptr, xt, GETPC(), store_tag1);
434 void HELPER(st2g_parallel)(CPUARMState *env, uint64_t ptr, uint64_t xt)
436 do_st2g(env, ptr, xt, GETPC(), store_tag1_parallel);
439 void HELPER(st2g_stub)(CPUARMState *env, uint64_t ptr)
441 int mmu_idx = cpu_mmu_index(env, false);
442 uintptr_t ra = GETPC();
443 int in_page = -(ptr | TARGET_PAGE_MASK);
445 check_tag_aligned(env, ptr, ra);
447 if (likely(in_page >= 2 * TAG_GRANULE)) {
448 probe_write(env, ptr, 2 * TAG_GRANULE, mmu_idx, ra);
449 } else {
450 probe_write(env, ptr, TAG_GRANULE, mmu_idx, ra);
451 probe_write(env, ptr + TAG_GRANULE, TAG_GRANULE, mmu_idx, ra);
455 #define LDGM_STGM_SIZE (4 << GMID_EL1_BS)
457 uint64_t HELPER(ldgm)(CPUARMState *env, uint64_t ptr)
459 int mmu_idx = cpu_mmu_index(env, false);
460 uintptr_t ra = GETPC();
461 void *tag_mem;
463 ptr = QEMU_ALIGN_DOWN(ptr, LDGM_STGM_SIZE);
465 /* Trap if accessing an invalid page. */
466 tag_mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_LOAD,
467 LDGM_STGM_SIZE, MMU_DATA_LOAD,
468 LDGM_STGM_SIZE / (2 * TAG_GRANULE), ra);
470 /* The tag is squashed to zero if the page does not support tags. */
471 if (!tag_mem) {
472 return 0;
475 QEMU_BUILD_BUG_ON(GMID_EL1_BS != 6);
477 * We are loading 64-bits worth of tags. The ordering of elements
478 * within the word corresponds to a 64-bit little-endian operation.
480 return ldq_le_p(tag_mem);
483 void HELPER(stgm)(CPUARMState *env, uint64_t ptr, uint64_t val)
485 int mmu_idx = cpu_mmu_index(env, false);
486 uintptr_t ra = GETPC();
487 void *tag_mem;
489 ptr = QEMU_ALIGN_DOWN(ptr, LDGM_STGM_SIZE);
491 /* Trap if accessing an invalid page. */
492 tag_mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE,
493 LDGM_STGM_SIZE, MMU_DATA_LOAD,
494 LDGM_STGM_SIZE / (2 * TAG_GRANULE), ra);
497 * Tag store only happens if the page support tags,
498 * and if the OS has enabled access to the tags.
500 if (!tag_mem) {
501 return;
504 QEMU_BUILD_BUG_ON(GMID_EL1_BS != 6);
506 * We are storing 64-bits worth of tags. The ordering of elements
507 * within the word corresponds to a 64-bit little-endian operation.
509 stq_le_p(tag_mem, val);
512 void HELPER(stzgm_tags)(CPUARMState *env, uint64_t ptr, uint64_t val)
514 uintptr_t ra = GETPC();
515 int mmu_idx = cpu_mmu_index(env, false);
516 int log2_dcz_bytes, log2_tag_bytes;
517 intptr_t dcz_bytes, tag_bytes;
518 uint8_t *mem;
521 * In arm_cpu_realizefn, we assert that dcz > LOG2_TAG_GRANULE+1,
522 * i.e. 32 bytes, which is an unreasonably small dcz anyway,
523 * to make sure that we can access one complete tag byte here.
525 log2_dcz_bytes = env_archcpu(env)->dcz_blocksize + 2;
526 log2_tag_bytes = log2_dcz_bytes - (LOG2_TAG_GRANULE + 1);
527 dcz_bytes = (intptr_t)1 << log2_dcz_bytes;
528 tag_bytes = (intptr_t)1 << log2_tag_bytes;
529 ptr &= -dcz_bytes;
531 mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE, dcz_bytes,
532 MMU_DATA_STORE, tag_bytes, ra);
533 if (mem) {
534 int tag_pair = (val & 0xf) * 0x11;
535 memset(mem, tag_pair, tag_bytes);
539 static void mte_sync_check_fail(CPUARMState *env, uint32_t desc,
540 uint64_t dirty_ptr, uintptr_t ra)
542 int is_write, syn;
544 env->exception.vaddress = dirty_ptr;
546 is_write = FIELD_EX32(desc, MTEDESC, WRITE);
547 syn = syn_data_abort_no_iss(arm_current_el(env) != 0, 0, 0, 0, 0, is_write,
548 0x11);
549 raise_exception_ra(env, EXCP_DATA_ABORT, syn, exception_target_el(env), ra);
550 g_assert_not_reached();
553 static void mte_async_check_fail(CPUARMState *env, uint64_t dirty_ptr,
554 uintptr_t ra, ARMMMUIdx arm_mmu_idx, int el)
556 int select;
558 if (regime_has_2_ranges(arm_mmu_idx)) {
559 select = extract64(dirty_ptr, 55, 1);
560 } else {
561 select = 0;
563 env->cp15.tfsr_el[el] |= 1 << select;
564 #ifdef CONFIG_USER_ONLY
566 * Stand in for a timer irq, setting _TIF_MTE_ASYNC_FAULT,
567 * which then sends a SIGSEGV when the thread is next scheduled.
568 * This cpu will return to the main loop at the end of the TB,
569 * which is rather sooner than "normal". But the alternative
570 * is waiting until the next syscall.
572 qemu_cpu_kick(env_cpu(env));
573 #endif
576 /* Record a tag check failure. */
577 static void mte_check_fail(CPUARMState *env, uint32_t desc,
578 uint64_t dirty_ptr, uintptr_t ra)
580 int mmu_idx = FIELD_EX32(desc, MTEDESC, MIDX);
581 ARMMMUIdx arm_mmu_idx = core_to_aa64_mmu_idx(mmu_idx);
582 int el, reg_el, tcf;
583 uint64_t sctlr;
585 reg_el = regime_el(env, arm_mmu_idx);
586 sctlr = env->cp15.sctlr_el[reg_el];
588 switch (arm_mmu_idx) {
589 case ARMMMUIdx_E10_0:
590 case ARMMMUIdx_E20_0:
591 el = 0;
592 tcf = extract64(sctlr, 38, 2);
593 break;
594 default:
595 el = reg_el;
596 tcf = extract64(sctlr, 40, 2);
599 switch (tcf) {
600 case 1:
601 /* Tag check fail causes a synchronous exception. */
602 mte_sync_check_fail(env, desc, dirty_ptr, ra);
603 break;
605 case 0:
607 * Tag check fail does not affect the PE.
608 * We eliminate this case by not setting MTE_ACTIVE
609 * in tb_flags, so that we never make this runtime call.
611 g_assert_not_reached();
613 case 2:
614 /* Tag check fail causes asynchronous flag set. */
615 mte_async_check_fail(env, dirty_ptr, ra, arm_mmu_idx, el);
616 break;
618 case 3:
620 * Tag check fail causes asynchronous flag set for stores, or
621 * a synchronous exception for loads.
623 if (FIELD_EX32(desc, MTEDESC, WRITE)) {
624 mte_async_check_fail(env, dirty_ptr, ra, arm_mmu_idx, el);
625 } else {
626 mte_sync_check_fail(env, desc, dirty_ptr, ra);
628 break;
633 * checkN:
634 * @tag: tag memory to test
635 * @odd: true to begin testing at tags at odd nibble
636 * @cmp: the tag to compare against
637 * @count: number of tags to test
639 * Return the number of successful tests.
640 * Thus a return value < @count indicates a failure.
642 * A note about sizes: count is expected to be small.
644 * The most common use will be LDP/STP of two integer registers,
645 * which means 16 bytes of memory touching at most 2 tags, but
646 * often the access is aligned and thus just 1 tag.
648 * Using AdvSIMD LD/ST (multiple), one can access 64 bytes of memory,
649 * touching at most 5 tags. SVE LDR/STR (vector) with the default
650 * vector length is also 64 bytes; the maximum architectural length
651 * is 256 bytes touching at most 9 tags.
653 * The loop below uses 7 logical operations and 1 memory operation
654 * per tag pair. An implementation that loads an aligned word and
655 * uses masking to ignore adjacent tags requires 18 logical operations
656 * and thus does not begin to pay off until 6 tags.
657 * Which, according to the survey above, is unlikely to be common.
659 static int checkN(uint8_t *mem, int odd, int cmp, int count)
661 int n = 0, diff;
663 /* Replicate the test tag and compare. */
664 cmp *= 0x11;
665 diff = *mem++ ^ cmp;
667 if (odd) {
668 goto start_odd;
671 while (1) {
672 /* Test even tag. */
673 if (unlikely((diff) & 0x0f)) {
674 break;
676 if (++n == count) {
677 break;
680 start_odd:
681 /* Test odd tag. */
682 if (unlikely((diff) & 0xf0)) {
683 break;
685 if (++n == count) {
686 break;
689 diff = *mem++ ^ cmp;
691 return n;
695 * mte_probe_int() - helper for mte_probe and mte_check
696 * @env: CPU environment
697 * @desc: MTEDESC descriptor
698 * @ptr: virtual address of the base of the access
699 * @fault: return virtual address of the first check failure
701 * Internal routine for both mte_probe and mte_check.
702 * Return zero on failure, filling in *fault.
703 * Return negative on trivial success for tbi disabled.
704 * Return positive on success with tbi enabled.
706 static int mte_probe_int(CPUARMState *env, uint32_t desc, uint64_t ptr,
707 uintptr_t ra, uint64_t *fault)
709 int mmu_idx, ptr_tag, bit55;
710 uint64_t ptr_last, prev_page, next_page;
711 uint64_t tag_first, tag_last;
712 uint64_t tag_byte_first, tag_byte_last;
713 uint32_t sizem1, tag_count, tag_size, n, c;
714 uint8_t *mem1, *mem2;
715 MMUAccessType type;
717 bit55 = extract64(ptr, 55, 1);
718 *fault = ptr;
720 /* If TBI is disabled, the access is unchecked, and ptr is not dirty. */
721 if (unlikely(!tbi_check(desc, bit55))) {
722 return -1;
725 ptr_tag = allocation_tag_from_addr(ptr);
727 if (tcma_check(desc, bit55, ptr_tag)) {
728 return 1;
731 mmu_idx = FIELD_EX32(desc, MTEDESC, MIDX);
732 type = FIELD_EX32(desc, MTEDESC, WRITE) ? MMU_DATA_STORE : MMU_DATA_LOAD;
733 sizem1 = FIELD_EX32(desc, MTEDESC, SIZEM1);
735 /* Find the addr of the end of the access */
736 ptr_last = ptr + sizem1;
738 /* Round the bounds to the tag granule, and compute the number of tags. */
739 tag_first = QEMU_ALIGN_DOWN(ptr, TAG_GRANULE);
740 tag_last = QEMU_ALIGN_DOWN(ptr_last, TAG_GRANULE);
741 tag_count = ((tag_last - tag_first) / TAG_GRANULE) + 1;
743 /* Round the bounds to twice the tag granule, and compute the bytes. */
744 tag_byte_first = QEMU_ALIGN_DOWN(ptr, 2 * TAG_GRANULE);
745 tag_byte_last = QEMU_ALIGN_DOWN(ptr_last, 2 * TAG_GRANULE);
747 /* Locate the page boundaries. */
748 prev_page = ptr & TARGET_PAGE_MASK;
749 next_page = prev_page + TARGET_PAGE_SIZE;
751 if (likely(tag_last - prev_page < TARGET_PAGE_SIZE)) {
752 /* Memory access stays on one page. */
753 tag_size = ((tag_byte_last - tag_byte_first) / (2 * TAG_GRANULE)) + 1;
754 mem1 = allocation_tag_mem(env, mmu_idx, ptr, type, sizem1 + 1,
755 MMU_DATA_LOAD, tag_size, ra);
756 if (!mem1) {
757 return 1;
759 /* Perform all of the comparisons. */
760 n = checkN(mem1, ptr & TAG_GRANULE, ptr_tag, tag_count);
761 } else {
762 /* Memory access crosses to next page. */
763 tag_size = (next_page - tag_byte_first) / (2 * TAG_GRANULE);
764 mem1 = allocation_tag_mem(env, mmu_idx, ptr, type, next_page - ptr,
765 MMU_DATA_LOAD, tag_size, ra);
767 tag_size = ((tag_byte_last - next_page) / (2 * TAG_GRANULE)) + 1;
768 mem2 = allocation_tag_mem(env, mmu_idx, next_page, type,
769 ptr_last - next_page + 1,
770 MMU_DATA_LOAD, tag_size, ra);
773 * Perform all of the comparisons.
774 * Note the possible but unlikely case of the operation spanning
775 * two pages that do not both have tagging enabled.
777 n = c = (next_page - tag_first) / TAG_GRANULE;
778 if (mem1) {
779 n = checkN(mem1, ptr & TAG_GRANULE, ptr_tag, c);
781 if (n == c) {
782 if (!mem2) {
783 return 1;
785 n += checkN(mem2, 0, ptr_tag, tag_count - c);
789 if (likely(n == tag_count)) {
790 return 1;
794 * If we failed, we know which granule. For the first granule, the
795 * failure address is @ptr, the first byte accessed. Otherwise the
796 * failure address is the first byte of the nth granule.
798 if (n > 0) {
799 *fault = tag_first + n * TAG_GRANULE;
801 return 0;
804 uint64_t mte_check(CPUARMState *env, uint32_t desc, uint64_t ptr, uintptr_t ra)
806 uint64_t fault;
807 int ret = mte_probe_int(env, desc, ptr, ra, &fault);
809 if (unlikely(ret == 0)) {
810 mte_check_fail(env, desc, fault, ra);
811 } else if (ret < 0) {
812 return ptr;
814 return useronly_clean_ptr(ptr);
817 uint64_t HELPER(mte_check)(CPUARMState *env, uint32_t desc, uint64_t ptr)
819 return mte_check(env, desc, ptr, GETPC());
823 * No-fault version of mte_check, to be used by SVE for MemSingleNF.
824 * Returns false if the access is Checked and the check failed. This
825 * is only intended to probe the tag -- the validity of the page must
826 * be checked beforehand.
828 bool mte_probe(CPUARMState *env, uint32_t desc, uint64_t ptr)
830 uint64_t fault;
831 int ret = mte_probe_int(env, desc, ptr, 0, &fault);
833 return ret != 0;
837 * Perform an MTE checked access for DC_ZVA.
839 uint64_t HELPER(mte_check_zva)(CPUARMState *env, uint32_t desc, uint64_t ptr)
841 uintptr_t ra = GETPC();
842 int log2_dcz_bytes, log2_tag_bytes;
843 int mmu_idx, bit55;
844 intptr_t dcz_bytes, tag_bytes, i;
845 void *mem;
846 uint64_t ptr_tag, mem_tag, align_ptr;
848 bit55 = extract64(ptr, 55, 1);
850 /* If TBI is disabled, the access is unchecked, and ptr is not dirty. */
851 if (unlikely(!tbi_check(desc, bit55))) {
852 return ptr;
855 ptr_tag = allocation_tag_from_addr(ptr);
857 if (tcma_check(desc, bit55, ptr_tag)) {
858 goto done;
862 * In arm_cpu_realizefn, we asserted that dcz > LOG2_TAG_GRANULE+1,
863 * i.e. 32 bytes, which is an unreasonably small dcz anyway, to make
864 * sure that we can access one complete tag byte here.
866 log2_dcz_bytes = env_archcpu(env)->dcz_blocksize + 2;
867 log2_tag_bytes = log2_dcz_bytes - (LOG2_TAG_GRANULE + 1);
868 dcz_bytes = (intptr_t)1 << log2_dcz_bytes;
869 tag_bytes = (intptr_t)1 << log2_tag_bytes;
870 align_ptr = ptr & -dcz_bytes;
873 * Trap if accessing an invalid page. DC_ZVA requires that we supply
874 * the original pointer for an invalid page. But watchpoints require
875 * that we probe the actual space. So do both.
877 mmu_idx = FIELD_EX32(desc, MTEDESC, MIDX);
878 (void) probe_write(env, ptr, 1, mmu_idx, ra);
879 mem = allocation_tag_mem(env, mmu_idx, align_ptr, MMU_DATA_STORE,
880 dcz_bytes, MMU_DATA_LOAD, tag_bytes, ra);
881 if (!mem) {
882 goto done;
886 * Unlike the reasoning for checkN, DC_ZVA is always aligned, and thus
887 * it is quite easy to perform all of the comparisons at once without
888 * any extra masking.
890 * The most common zva block size is 64; some of the thunderx cpus use
891 * a block size of 128. For user-only, aarch64_max_initfn will set the
892 * block size to 512. Fill out the other cases for future-proofing.
894 * In order to be able to find the first miscompare later, we want the
895 * tag bytes to be in little-endian order.
897 switch (log2_tag_bytes) {
898 case 0: /* zva_blocksize 32 */
899 mem_tag = *(uint8_t *)mem;
900 ptr_tag *= 0x11u;
901 break;
902 case 1: /* zva_blocksize 64 */
903 mem_tag = cpu_to_le16(*(uint16_t *)mem);
904 ptr_tag *= 0x1111u;
905 break;
906 case 2: /* zva_blocksize 128 */
907 mem_tag = cpu_to_le32(*(uint32_t *)mem);
908 ptr_tag *= 0x11111111u;
909 break;
910 case 3: /* zva_blocksize 256 */
911 mem_tag = cpu_to_le64(*(uint64_t *)mem);
912 ptr_tag *= 0x1111111111111111ull;
913 break;
915 default: /* zva_blocksize 512, 1024, 2048 */
916 ptr_tag *= 0x1111111111111111ull;
917 i = 0;
918 do {
919 mem_tag = cpu_to_le64(*(uint64_t *)(mem + i));
920 if (unlikely(mem_tag != ptr_tag)) {
921 goto fail;
923 i += 8;
924 align_ptr += 16 * TAG_GRANULE;
925 } while (i < tag_bytes);
926 goto done;
929 if (likely(mem_tag == ptr_tag)) {
930 goto done;
933 fail:
934 /* Locate the first nibble that differs. */
935 i = ctz64(mem_tag ^ ptr_tag) >> 4;
936 mte_check_fail(env, desc, align_ptr + i * TAG_GRANULE, ra);
938 done:
939 return useronly_clean_ptr(ptr);