exec/exec-all: Move 'qemu/log.h' include in units requiring it
[qemu/rayw.git] / target / arm / mte_helper.c
blobd11a8c70d041622f64e02dc8df1e08880331cc70
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 "qemu/log.h"
22 #include "cpu.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) {
35 return 0;
37 if (offset == 0) {
38 while (exclude & (1 << tag)) {
39 tag = (tag + 1) & 15;
41 } else {
42 do {
43 do {
44 tag = (tag + 1) & 15;
45 } while (exclude & (1 << tag));
46 } while (--offset > 0);
48 return tag;
51 /**
52 * allocation_tag_mem:
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);
84 uint8_t *tags;
85 uintptr_t index;
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)) {
94 return NULL;
97 tags = page_get_target_data(clean_ptr);
98 if (tags == NULL) {
99 size_t alloc_size = TARGET_PAGE_SIZE >> (LOG2_TAG_GRANULE + 1);
100 tags = page_alloc_target_data(clean_ptr, alloc_size);
101 assert(tags != NULL);
104 index = extract32(ptr, LOG2_TAG_GRANULE + 1,
105 TARGET_PAGE_BITS - LOG2_TAG_GRANULE - 1);
106 return tags + index;
107 #else
108 uintptr_t index;
109 CPUIOTLBEntry *iotlbentry;
110 int in_page, flags;
111 ram_addr_t ptr_ra;
112 hwaddr ptr_paddr, tag_paddr, xlat;
113 MemoryRegion *mr;
114 ARMASIdx tag_asi;
115 AddressSpace *tag_as;
116 void *host;
119 * Probe the first byte of the virtual address. This raises an
120 * exception for inaccessible pages, and resolves the virtual address
121 * into the softmmu tlb.
123 * When RA == 0, this is for mte_probe. The page is expected to be
124 * valid. Indicate to probe_access_flags no-fault, then assert that
125 * we received a valid page.
127 flags = probe_access_flags(env, ptr, ptr_access, ptr_mmu_idx,
128 ra == 0, &host, ra);
129 assert(!(flags & TLB_INVALID_MASK));
132 * Find the iotlbentry for ptr. This *must* be present in the TLB
133 * because we just found the mapping.
134 * TODO: Perhaps there should be a cputlb helper that returns a
135 * matching tlb entry + iotlb entry.
137 index = tlb_index(env, ptr_mmu_idx, ptr);
138 # ifdef CONFIG_DEBUG_TCG
140 CPUTLBEntry *entry = tlb_entry(env, ptr_mmu_idx, ptr);
141 target_ulong comparator = (ptr_access == MMU_DATA_LOAD
142 ? entry->addr_read
143 : tlb_addr_write(entry));
144 g_assert(tlb_hit(comparator, ptr));
146 # endif
147 iotlbentry = &env_tlb(env)->d[ptr_mmu_idx].iotlb[index];
149 /* If the virtual page MemAttr != Tagged, access unchecked. */
150 if (!arm_tlb_mte_tagged(&iotlbentry->attrs)) {
151 return NULL;
155 * If not backed by host ram, there is no tag storage: access unchecked.
156 * This is probably a guest os bug though, so log it.
158 if (unlikely(flags & TLB_MMIO)) {
159 qemu_log_mask(LOG_GUEST_ERROR,
160 "Page @ 0x%" PRIx64 " indicates Tagged Normal memory "
161 "but is not backed by host ram\n", ptr);
162 return NULL;
166 * The Normal memory access can extend to the next page. E.g. a single
167 * 8-byte access to the last byte of a page will check only the last
168 * tag on the first page.
169 * Any page access exception has priority over tag check exception.
171 in_page = -(ptr | TARGET_PAGE_MASK);
172 if (unlikely(ptr_size > in_page)) {
173 void *ignore;
174 flags |= probe_access_flags(env, ptr + in_page, ptr_access,
175 ptr_mmu_idx, ra == 0, &ignore, ra);
176 assert(!(flags & TLB_INVALID_MASK));
179 /* Any debug exception has priority over a tag check exception. */
180 if (unlikely(flags & TLB_WATCHPOINT)) {
181 int wp = ptr_access == MMU_DATA_LOAD ? BP_MEM_READ : BP_MEM_WRITE;
182 assert(ra != 0);
183 cpu_check_watchpoint(env_cpu(env), ptr, ptr_size,
184 iotlbentry->attrs, wp, ra);
188 * Find the physical address within the normal mem space.
189 * The memory region lookup must succeed because TLB_MMIO was
190 * not set in the cputlb lookup above.
192 mr = memory_region_from_host(host, &ptr_ra);
193 tcg_debug_assert(mr != NULL);
194 tcg_debug_assert(memory_region_is_ram(mr));
195 ptr_paddr = ptr_ra;
196 do {
197 ptr_paddr += mr->addr;
198 mr = mr->container;
199 } while (mr);
201 /* Convert to the physical address in tag space. */
202 tag_paddr = ptr_paddr >> (LOG2_TAG_GRANULE + 1);
204 /* Look up the address in tag space. */
205 tag_asi = iotlbentry->attrs.secure ? ARMASIdx_TagS : ARMASIdx_TagNS;
206 tag_as = cpu_get_address_space(env_cpu(env), tag_asi);
207 mr = address_space_translate(tag_as, tag_paddr, &xlat, NULL,
208 tag_access == MMU_DATA_STORE,
209 iotlbentry->attrs);
212 * Note that @mr will never be NULL. If there is nothing in the address
213 * space at @tag_paddr, the translation will return the unallocated memory
214 * region. For our purposes, the result must be ram.
216 if (unlikely(!memory_region_is_ram(mr))) {
217 /* ??? Failure is a board configuration error. */
218 qemu_log_mask(LOG_UNIMP,
219 "Tag Memory @ 0x%" HWADDR_PRIx " not found for "
220 "Normal Memory @ 0x%" HWADDR_PRIx "\n",
221 tag_paddr, ptr_paddr);
222 return NULL;
226 * Ensure the tag memory is dirty on write, for migration.
227 * Tag memory can never contain code or display memory (vga).
229 if (tag_access == MMU_DATA_STORE) {
230 ram_addr_t tag_ra = memory_region_get_ram_addr(mr) + xlat;
231 cpu_physical_memory_set_dirty_flag(tag_ra, DIRTY_MEMORY_MIGRATION);
234 return memory_region_get_ram_ptr(mr) + xlat;
235 #endif
238 uint64_t HELPER(irg)(CPUARMState *env, uint64_t rn, uint64_t rm)
240 uint16_t exclude = extract32(rm | env->cp15.gcr_el1, 0, 16);
241 int rrnd = extract32(env->cp15.gcr_el1, 16, 1);
242 int start = extract32(env->cp15.rgsr_el1, 0, 4);
243 int seed = extract32(env->cp15.rgsr_el1, 8, 16);
244 int offset, i, rtag;
247 * Our IMPDEF choice for GCR_EL1.RRND==1 is to continue to use the
248 * deterministic algorithm. Except that with RRND==1 the kernel is
249 * not required to have set RGSR_EL1.SEED != 0, which is required for
250 * the deterministic algorithm to function. So we force a non-zero
251 * SEED for that case.
253 if (unlikely(seed == 0) && rrnd) {
254 do {
255 Error *err = NULL;
256 uint16_t two;
258 if (qemu_guest_getrandom(&two, sizeof(two), &err) < 0) {
260 * Failed, for unknown reasons in the crypto subsystem.
261 * Best we can do is log the reason and use a constant seed.
263 qemu_log_mask(LOG_UNIMP, "IRG: Crypto failure: %s\n",
264 error_get_pretty(err));
265 error_free(err);
266 two = 1;
268 seed = two;
269 } while (seed == 0);
272 /* RandomTag */
273 for (i = offset = 0; i < 4; ++i) {
274 /* NextRandomTagBit */
275 int top = (extract32(seed, 5, 1) ^ extract32(seed, 3, 1) ^
276 extract32(seed, 2, 1) ^ extract32(seed, 0, 1));
277 seed = (top << 15) | (seed >> 1);
278 offset |= top << i;
280 rtag = choose_nonexcluded_tag(start, offset, exclude);
281 env->cp15.rgsr_el1 = rtag | (seed << 8);
283 return address_with_allocation_tag(rn, rtag);
286 uint64_t HELPER(addsubg)(CPUARMState *env, uint64_t ptr,
287 int32_t offset, uint32_t tag_offset)
289 int start_tag = allocation_tag_from_addr(ptr);
290 uint16_t exclude = extract32(env->cp15.gcr_el1, 0, 16);
291 int rtag = choose_nonexcluded_tag(start_tag, tag_offset, exclude);
293 return address_with_allocation_tag(ptr + offset, rtag);
296 static int load_tag1(uint64_t ptr, uint8_t *mem)
298 int ofs = extract32(ptr, LOG2_TAG_GRANULE, 1) * 4;
299 return extract32(*mem, ofs, 4);
302 uint64_t HELPER(ldg)(CPUARMState *env, uint64_t ptr, uint64_t xt)
304 int mmu_idx = cpu_mmu_index(env, false);
305 uint8_t *mem;
306 int rtag = 0;
308 /* Trap if accessing an invalid page. */
309 mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_LOAD, 1,
310 MMU_DATA_LOAD, 1, GETPC());
312 /* Load if page supports tags. */
313 if (mem) {
314 rtag = load_tag1(ptr, mem);
317 return address_with_allocation_tag(xt, rtag);
320 static void check_tag_aligned(CPUARMState *env, uint64_t ptr, uintptr_t ra)
322 if (unlikely(!QEMU_IS_ALIGNED(ptr, TAG_GRANULE))) {
323 arm_cpu_do_unaligned_access(env_cpu(env), ptr, MMU_DATA_STORE,
324 cpu_mmu_index(env, false), ra);
325 g_assert_not_reached();
329 /* For use in a non-parallel context, store to the given nibble. */
330 static void store_tag1(uint64_t ptr, uint8_t *mem, int tag)
332 int ofs = extract32(ptr, LOG2_TAG_GRANULE, 1) * 4;
333 *mem = deposit32(*mem, ofs, 4, tag);
336 /* For use in a parallel context, atomically store to the given nibble. */
337 static void store_tag1_parallel(uint64_t ptr, uint8_t *mem, int tag)
339 int ofs = extract32(ptr, LOG2_TAG_GRANULE, 1) * 4;
340 uint8_t old = qatomic_read(mem);
342 while (1) {
343 uint8_t new = deposit32(old, ofs, 4, tag);
344 uint8_t cmp = qatomic_cmpxchg(mem, old, new);
345 if (likely(cmp == old)) {
346 return;
348 old = cmp;
352 typedef void stg_store1(uint64_t, uint8_t *, int);
354 static inline void do_stg(CPUARMState *env, uint64_t ptr, uint64_t xt,
355 uintptr_t ra, stg_store1 store1)
357 int mmu_idx = cpu_mmu_index(env, false);
358 uint8_t *mem;
360 check_tag_aligned(env, ptr, ra);
362 /* Trap if accessing an invalid page. */
363 mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE, TAG_GRANULE,
364 MMU_DATA_STORE, 1, ra);
366 /* Store if page supports tags. */
367 if (mem) {
368 store1(ptr, mem, allocation_tag_from_addr(xt));
372 void HELPER(stg)(CPUARMState *env, uint64_t ptr, uint64_t xt)
374 do_stg(env, ptr, xt, GETPC(), store_tag1);
377 void HELPER(stg_parallel)(CPUARMState *env, uint64_t ptr, uint64_t xt)
379 do_stg(env, ptr, xt, GETPC(), store_tag1_parallel);
382 void HELPER(stg_stub)(CPUARMState *env, uint64_t ptr)
384 int mmu_idx = cpu_mmu_index(env, false);
385 uintptr_t ra = GETPC();
387 check_tag_aligned(env, ptr, ra);
388 probe_write(env, ptr, TAG_GRANULE, mmu_idx, ra);
391 static inline void do_st2g(CPUARMState *env, uint64_t ptr, uint64_t xt,
392 uintptr_t ra, stg_store1 store1)
394 int mmu_idx = cpu_mmu_index(env, false);
395 int tag = allocation_tag_from_addr(xt);
396 uint8_t *mem1, *mem2;
398 check_tag_aligned(env, ptr, ra);
401 * Trap if accessing an invalid page(s).
402 * This takes priority over !allocation_tag_access_enabled.
404 if (ptr & TAG_GRANULE) {
405 /* Two stores unaligned mod TAG_GRANULE*2 -- modify two bytes. */
406 mem1 = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE,
407 TAG_GRANULE, MMU_DATA_STORE, 1, ra);
408 mem2 = allocation_tag_mem(env, mmu_idx, ptr + TAG_GRANULE,
409 MMU_DATA_STORE, TAG_GRANULE,
410 MMU_DATA_STORE, 1, ra);
412 /* Store if page(s) support tags. */
413 if (mem1) {
414 store1(TAG_GRANULE, mem1, tag);
416 if (mem2) {
417 store1(0, mem2, tag);
419 } else {
420 /* Two stores aligned mod TAG_GRANULE*2 -- modify one byte. */
421 mem1 = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE,
422 2 * TAG_GRANULE, MMU_DATA_STORE, 1, ra);
423 if (mem1) {
424 tag |= tag << 4;
425 qatomic_set(mem1, tag);
430 void HELPER(st2g)(CPUARMState *env, uint64_t ptr, uint64_t xt)
432 do_st2g(env, ptr, xt, GETPC(), store_tag1);
435 void HELPER(st2g_parallel)(CPUARMState *env, uint64_t ptr, uint64_t xt)
437 do_st2g(env, ptr, xt, GETPC(), store_tag1_parallel);
440 void HELPER(st2g_stub)(CPUARMState *env, uint64_t ptr)
442 int mmu_idx = cpu_mmu_index(env, false);
443 uintptr_t ra = GETPC();
444 int in_page = -(ptr | TARGET_PAGE_MASK);
446 check_tag_aligned(env, ptr, ra);
448 if (likely(in_page >= 2 * TAG_GRANULE)) {
449 probe_write(env, ptr, 2 * TAG_GRANULE, mmu_idx, ra);
450 } else {
451 probe_write(env, ptr, TAG_GRANULE, mmu_idx, ra);
452 probe_write(env, ptr + TAG_GRANULE, TAG_GRANULE, mmu_idx, ra);
456 #define LDGM_STGM_SIZE (4 << GMID_EL1_BS)
458 uint64_t HELPER(ldgm)(CPUARMState *env, uint64_t ptr)
460 int mmu_idx = cpu_mmu_index(env, false);
461 uintptr_t ra = GETPC();
462 void *tag_mem;
464 ptr = QEMU_ALIGN_DOWN(ptr, LDGM_STGM_SIZE);
466 /* Trap if accessing an invalid page. */
467 tag_mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_LOAD,
468 LDGM_STGM_SIZE, MMU_DATA_LOAD,
469 LDGM_STGM_SIZE / (2 * TAG_GRANULE), ra);
471 /* The tag is squashed to zero if the page does not support tags. */
472 if (!tag_mem) {
473 return 0;
476 QEMU_BUILD_BUG_ON(GMID_EL1_BS != 6);
478 * We are loading 64-bits worth of tags. The ordering of elements
479 * within the word corresponds to a 64-bit little-endian operation.
481 return ldq_le_p(tag_mem);
484 void HELPER(stgm)(CPUARMState *env, uint64_t ptr, uint64_t val)
486 int mmu_idx = cpu_mmu_index(env, false);
487 uintptr_t ra = GETPC();
488 void *tag_mem;
490 ptr = QEMU_ALIGN_DOWN(ptr, LDGM_STGM_SIZE);
492 /* Trap if accessing an invalid page. */
493 tag_mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE,
494 LDGM_STGM_SIZE, MMU_DATA_LOAD,
495 LDGM_STGM_SIZE / (2 * TAG_GRANULE), ra);
498 * Tag store only happens if the page support tags,
499 * and if the OS has enabled access to the tags.
501 if (!tag_mem) {
502 return;
505 QEMU_BUILD_BUG_ON(GMID_EL1_BS != 6);
507 * We are storing 64-bits worth of tags. The ordering of elements
508 * within the word corresponds to a 64-bit little-endian operation.
510 stq_le_p(tag_mem, val);
513 void HELPER(stzgm_tags)(CPUARMState *env, uint64_t ptr, uint64_t val)
515 uintptr_t ra = GETPC();
516 int mmu_idx = cpu_mmu_index(env, false);
517 int log2_dcz_bytes, log2_tag_bytes;
518 intptr_t dcz_bytes, tag_bytes;
519 uint8_t *mem;
522 * In arm_cpu_realizefn, we assert that dcz > LOG2_TAG_GRANULE+1,
523 * i.e. 32 bytes, which is an unreasonably small dcz anyway,
524 * to make sure that we can access one complete tag byte here.
526 log2_dcz_bytes = env_archcpu(env)->dcz_blocksize + 2;
527 log2_tag_bytes = log2_dcz_bytes - (LOG2_TAG_GRANULE + 1);
528 dcz_bytes = (intptr_t)1 << log2_dcz_bytes;
529 tag_bytes = (intptr_t)1 << log2_tag_bytes;
530 ptr &= -dcz_bytes;
532 mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE, dcz_bytes,
533 MMU_DATA_STORE, tag_bytes, ra);
534 if (mem) {
535 int tag_pair = (val & 0xf) * 0x11;
536 memset(mem, tag_pair, tag_bytes);
540 static void mte_sync_check_fail(CPUARMState *env, uint32_t desc,
541 uint64_t dirty_ptr, uintptr_t ra)
543 int is_write, syn;
545 env->exception.vaddress = dirty_ptr;
547 is_write = FIELD_EX32(desc, MTEDESC, WRITE);
548 syn = syn_data_abort_no_iss(arm_current_el(env) != 0, 0, 0, 0, 0, is_write,
549 0x11);
550 raise_exception_ra(env, EXCP_DATA_ABORT, syn, exception_target_el(env), ra);
551 g_assert_not_reached();
554 static void mte_async_check_fail(CPUARMState *env, uint64_t dirty_ptr,
555 uintptr_t ra, ARMMMUIdx arm_mmu_idx, int el)
557 int select;
559 if (regime_has_2_ranges(arm_mmu_idx)) {
560 select = extract64(dirty_ptr, 55, 1);
561 } else {
562 select = 0;
564 env->cp15.tfsr_el[el] |= 1 << select;
565 #ifdef CONFIG_USER_ONLY
567 * Stand in for a timer irq, setting _TIF_MTE_ASYNC_FAULT,
568 * which then sends a SIGSEGV when the thread is next scheduled.
569 * This cpu will return to the main loop at the end of the TB,
570 * which is rather sooner than "normal". But the alternative
571 * is waiting until the next syscall.
573 qemu_cpu_kick(env_cpu(env));
574 #endif
577 /* Record a tag check failure. */
578 static void mte_check_fail(CPUARMState *env, uint32_t desc,
579 uint64_t dirty_ptr, uintptr_t ra)
581 int mmu_idx = FIELD_EX32(desc, MTEDESC, MIDX);
582 ARMMMUIdx arm_mmu_idx = core_to_aa64_mmu_idx(mmu_idx);
583 int el, reg_el, tcf;
584 uint64_t sctlr;
586 reg_el = regime_el(env, arm_mmu_idx);
587 sctlr = env->cp15.sctlr_el[reg_el];
589 switch (arm_mmu_idx) {
590 case ARMMMUIdx_E10_0:
591 case ARMMMUIdx_E20_0:
592 el = 0;
593 tcf = extract64(sctlr, 38, 2);
594 break;
595 default:
596 el = reg_el;
597 tcf = extract64(sctlr, 40, 2);
600 switch (tcf) {
601 case 1:
602 /* Tag check fail causes a synchronous exception. */
603 mte_sync_check_fail(env, desc, dirty_ptr, ra);
604 break;
606 case 0:
608 * Tag check fail does not affect the PE.
609 * We eliminate this case by not setting MTE_ACTIVE
610 * in tb_flags, so that we never make this runtime call.
612 g_assert_not_reached();
614 case 2:
615 /* Tag check fail causes asynchronous flag set. */
616 mte_async_check_fail(env, dirty_ptr, ra, arm_mmu_idx, el);
617 break;
619 case 3:
621 * Tag check fail causes asynchronous flag set for stores, or
622 * a synchronous exception for loads.
624 if (FIELD_EX32(desc, MTEDESC, WRITE)) {
625 mte_async_check_fail(env, dirty_ptr, ra, arm_mmu_idx, el);
626 } else {
627 mte_sync_check_fail(env, desc, dirty_ptr, ra);
629 break;
634 * checkN:
635 * @tag: tag memory to test
636 * @odd: true to begin testing at tags at odd nibble
637 * @cmp: the tag to compare against
638 * @count: number of tags to test
640 * Return the number of successful tests.
641 * Thus a return value < @count indicates a failure.
643 * A note about sizes: count is expected to be small.
645 * The most common use will be LDP/STP of two integer registers,
646 * which means 16 bytes of memory touching at most 2 tags, but
647 * often the access is aligned and thus just 1 tag.
649 * Using AdvSIMD LD/ST (multiple), one can access 64 bytes of memory,
650 * touching at most 5 tags. SVE LDR/STR (vector) with the default
651 * vector length is also 64 bytes; the maximum architectural length
652 * is 256 bytes touching at most 9 tags.
654 * The loop below uses 7 logical operations and 1 memory operation
655 * per tag pair. An implementation that loads an aligned word and
656 * uses masking to ignore adjacent tags requires 18 logical operations
657 * and thus does not begin to pay off until 6 tags.
658 * Which, according to the survey above, is unlikely to be common.
660 static int checkN(uint8_t *mem, int odd, int cmp, int count)
662 int n = 0, diff;
664 /* Replicate the test tag and compare. */
665 cmp *= 0x11;
666 diff = *mem++ ^ cmp;
668 if (odd) {
669 goto start_odd;
672 while (1) {
673 /* Test even tag. */
674 if (unlikely((diff) & 0x0f)) {
675 break;
677 if (++n == count) {
678 break;
681 start_odd:
682 /* Test odd tag. */
683 if (unlikely((diff) & 0xf0)) {
684 break;
686 if (++n == count) {
687 break;
690 diff = *mem++ ^ cmp;
692 return n;
696 * mte_probe_int() - helper for mte_probe and mte_check
697 * @env: CPU environment
698 * @desc: MTEDESC descriptor
699 * @ptr: virtual address of the base of the access
700 * @fault: return virtual address of the first check failure
702 * Internal routine for both mte_probe and mte_check.
703 * Return zero on failure, filling in *fault.
704 * Return negative on trivial success for tbi disabled.
705 * Return positive on success with tbi enabled.
707 static int mte_probe_int(CPUARMState *env, uint32_t desc, uint64_t ptr,
708 uintptr_t ra, uint64_t *fault)
710 int mmu_idx, ptr_tag, bit55;
711 uint64_t ptr_last, prev_page, next_page;
712 uint64_t tag_first, tag_last;
713 uint64_t tag_byte_first, tag_byte_last;
714 uint32_t sizem1, tag_count, tag_size, n, c;
715 uint8_t *mem1, *mem2;
716 MMUAccessType type;
718 bit55 = extract64(ptr, 55, 1);
719 *fault = ptr;
721 /* If TBI is disabled, the access is unchecked, and ptr is not dirty. */
722 if (unlikely(!tbi_check(desc, bit55))) {
723 return -1;
726 ptr_tag = allocation_tag_from_addr(ptr);
728 if (tcma_check(desc, bit55, ptr_tag)) {
729 return 1;
732 mmu_idx = FIELD_EX32(desc, MTEDESC, MIDX);
733 type = FIELD_EX32(desc, MTEDESC, WRITE) ? MMU_DATA_STORE : MMU_DATA_LOAD;
734 sizem1 = FIELD_EX32(desc, MTEDESC, SIZEM1);
736 /* Find the addr of the end of the access */
737 ptr_last = ptr + sizem1;
739 /* Round the bounds to the tag granule, and compute the number of tags. */
740 tag_first = QEMU_ALIGN_DOWN(ptr, TAG_GRANULE);
741 tag_last = QEMU_ALIGN_DOWN(ptr_last, TAG_GRANULE);
742 tag_count = ((tag_last - tag_first) / TAG_GRANULE) + 1;
744 /* Round the bounds to twice the tag granule, and compute the bytes. */
745 tag_byte_first = QEMU_ALIGN_DOWN(ptr, 2 * TAG_GRANULE);
746 tag_byte_last = QEMU_ALIGN_DOWN(ptr_last, 2 * TAG_GRANULE);
748 /* Locate the page boundaries. */
749 prev_page = ptr & TARGET_PAGE_MASK;
750 next_page = prev_page + TARGET_PAGE_SIZE;
752 if (likely(tag_last - prev_page < TARGET_PAGE_SIZE)) {
753 /* Memory access stays on one page. */
754 tag_size = ((tag_byte_last - tag_byte_first) / (2 * TAG_GRANULE)) + 1;
755 mem1 = allocation_tag_mem(env, mmu_idx, ptr, type, sizem1 + 1,
756 MMU_DATA_LOAD, tag_size, ra);
757 if (!mem1) {
758 return 1;
760 /* Perform all of the comparisons. */
761 n = checkN(mem1, ptr & TAG_GRANULE, ptr_tag, tag_count);
762 } else {
763 /* Memory access crosses to next page. */
764 tag_size = (next_page - tag_byte_first) / (2 * TAG_GRANULE);
765 mem1 = allocation_tag_mem(env, mmu_idx, ptr, type, next_page - ptr,
766 MMU_DATA_LOAD, tag_size, ra);
768 tag_size = ((tag_byte_last - next_page) / (2 * TAG_GRANULE)) + 1;
769 mem2 = allocation_tag_mem(env, mmu_idx, next_page, type,
770 ptr_last - next_page + 1,
771 MMU_DATA_LOAD, tag_size, ra);
774 * Perform all of the comparisons.
775 * Note the possible but unlikely case of the operation spanning
776 * two pages that do not both have tagging enabled.
778 n = c = (next_page - tag_first) / TAG_GRANULE;
779 if (mem1) {
780 n = checkN(mem1, ptr & TAG_GRANULE, ptr_tag, c);
782 if (n == c) {
783 if (!mem2) {
784 return 1;
786 n += checkN(mem2, 0, ptr_tag, tag_count - c);
790 if (likely(n == tag_count)) {
791 return 1;
795 * If we failed, we know which granule. For the first granule, the
796 * failure address is @ptr, the first byte accessed. Otherwise the
797 * failure address is the first byte of the nth granule.
799 if (n > 0) {
800 *fault = tag_first + n * TAG_GRANULE;
802 return 0;
805 uint64_t mte_check(CPUARMState *env, uint32_t desc, uint64_t ptr, uintptr_t ra)
807 uint64_t fault;
808 int ret = mte_probe_int(env, desc, ptr, ra, &fault);
810 if (unlikely(ret == 0)) {
811 mte_check_fail(env, desc, fault, ra);
812 } else if (ret < 0) {
813 return ptr;
815 return useronly_clean_ptr(ptr);
818 uint64_t HELPER(mte_check)(CPUARMState *env, uint32_t desc, uint64_t ptr)
820 return mte_check(env, desc, ptr, GETPC());
824 * No-fault version of mte_check, to be used by SVE for MemSingleNF.
825 * Returns false if the access is Checked and the check failed. This
826 * is only intended to probe the tag -- the validity of the page must
827 * be checked beforehand.
829 bool mte_probe(CPUARMState *env, uint32_t desc, uint64_t ptr)
831 uint64_t fault;
832 int ret = mte_probe_int(env, desc, ptr, 0, &fault);
834 return ret != 0;
838 * Perform an MTE checked access for DC_ZVA.
840 uint64_t HELPER(mte_check_zva)(CPUARMState *env, uint32_t desc, uint64_t ptr)
842 uintptr_t ra = GETPC();
843 int log2_dcz_bytes, log2_tag_bytes;
844 int mmu_idx, bit55;
845 intptr_t dcz_bytes, tag_bytes, i;
846 void *mem;
847 uint64_t ptr_tag, mem_tag, align_ptr;
849 bit55 = extract64(ptr, 55, 1);
851 /* If TBI is disabled, the access is unchecked, and ptr is not dirty. */
852 if (unlikely(!tbi_check(desc, bit55))) {
853 return ptr;
856 ptr_tag = allocation_tag_from_addr(ptr);
858 if (tcma_check(desc, bit55, ptr_tag)) {
859 goto done;
863 * In arm_cpu_realizefn, we asserted that dcz > LOG2_TAG_GRANULE+1,
864 * i.e. 32 bytes, which is an unreasonably small dcz anyway, to make
865 * sure that we can access one complete tag byte here.
867 log2_dcz_bytes = env_archcpu(env)->dcz_blocksize + 2;
868 log2_tag_bytes = log2_dcz_bytes - (LOG2_TAG_GRANULE + 1);
869 dcz_bytes = (intptr_t)1 << log2_dcz_bytes;
870 tag_bytes = (intptr_t)1 << log2_tag_bytes;
871 align_ptr = ptr & -dcz_bytes;
874 * Trap if accessing an invalid page. DC_ZVA requires that we supply
875 * the original pointer for an invalid page. But watchpoints require
876 * that we probe the actual space. So do both.
878 mmu_idx = FIELD_EX32(desc, MTEDESC, MIDX);
879 (void) probe_write(env, ptr, 1, mmu_idx, ra);
880 mem = allocation_tag_mem(env, mmu_idx, align_ptr, MMU_DATA_STORE,
881 dcz_bytes, MMU_DATA_LOAD, tag_bytes, ra);
882 if (!mem) {
883 goto done;
887 * Unlike the reasoning for checkN, DC_ZVA is always aligned, and thus
888 * it is quite easy to perform all of the comparisons at once without
889 * any extra masking.
891 * The most common zva block size is 64; some of the thunderx cpus use
892 * a block size of 128. For user-only, aarch64_max_initfn will set the
893 * block size to 512. Fill out the other cases for future-proofing.
895 * In order to be able to find the first miscompare later, we want the
896 * tag bytes to be in little-endian order.
898 switch (log2_tag_bytes) {
899 case 0: /* zva_blocksize 32 */
900 mem_tag = *(uint8_t *)mem;
901 ptr_tag *= 0x11u;
902 break;
903 case 1: /* zva_blocksize 64 */
904 mem_tag = cpu_to_le16(*(uint16_t *)mem);
905 ptr_tag *= 0x1111u;
906 break;
907 case 2: /* zva_blocksize 128 */
908 mem_tag = cpu_to_le32(*(uint32_t *)mem);
909 ptr_tag *= 0x11111111u;
910 break;
911 case 3: /* zva_blocksize 256 */
912 mem_tag = cpu_to_le64(*(uint64_t *)mem);
913 ptr_tag *= 0x1111111111111111ull;
914 break;
916 default: /* zva_blocksize 512, 1024, 2048 */
917 ptr_tag *= 0x1111111111111111ull;
918 i = 0;
919 do {
920 mem_tag = cpu_to_le64(*(uint64_t *)(mem + i));
921 if (unlikely(mem_tag != ptr_tag)) {
922 goto fail;
924 i += 8;
925 align_ptr += 16 * TAG_GRANULE;
926 } while (i < tag_bytes);
927 goto done;
930 if (likely(mem_tag == ptr_tag)) {
931 goto done;
934 fail:
935 /* Locate the first nibble that differs. */
936 i = ctz64(mem_tag ^ ptr_tag) >> 4;
937 mte_check_fail(env, desc, align_ptr + i * TAG_GRANULE, ra);
939 done:
940 return useronly_clean_ptr(ptr);