1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2020 ARM Ltd.
5 #include <linux/linkage.h>
7 #include <asm/asm-uaccess.h>
8 #include <asm/assembler.h>
11 #include <asm/sysreg.h>
13 .arch armv8.5-a+memtag
16 * multitag_transfer_size - set \reg to the block size that is accessed by the
17 * LDGM/STGM instructions.
19 .macro multitag_transfer_size, reg, tmp
20 mrs_s \reg, SYS_GMID_EL1
21 ubfx \reg, \reg, #SYS_GMID_EL1_BS_SHIFT, #SYS_GMID_EL1_BS_SIZE
27 * Clear the tags in a page
28 * x0 - address of the page to be cleared
30 SYM_FUNC_START(mte_clear_page_tags)
31 multitag_transfer_size x1, x2
34 tst x0, #(PAGE_SIZE - 1)
37 SYM_FUNC_END(mte_clear_page_tags)
40 * Copy the tags from the source page to the destination one
41 * x0 - address of the destination page
42 * x1 - address of the source page
44 SYM_FUNC_START(mte_copy_page_tags)
47 multitag_transfer_size x5, x6
52 tst x2, #(PAGE_SIZE - 1)
55 SYM_FUNC_END(mte_copy_page_tags)
58 * Read tags from a user buffer (one tag per byte) and set the corresponding
59 * tags at the given kernel address. Used by PTRACE_POKEMTETAGS.
60 * x0 - kernel address (to)
61 * x1 - user buffer (from)
62 * x2 - number of tags/bytes (n)
64 * x0 - number of tags read/set
66 SYM_FUNC_START(mte_copy_tags_from_user)
70 user_ldst 2f, ldtrb, w4, x1, 0
71 lsl x4, x4, #MTE_TAG_SHIFT
72 stg x4, [x0], #MTE_GRANULE_SIZE
77 // exception handling and function return
78 2: sub x0, x1, x3 // update the number of tags set
80 SYM_FUNC_END(mte_copy_tags_from_user)
83 * Get the tags from a kernel address range and write the tag values to the
84 * given user buffer (one tag per byte). Used by PTRACE_PEEKMTETAGS.
85 * x0 - user buffer (to)
86 * x1 - kernel address (from)
87 * x2 - number of tags/bytes (n)
89 * x0 - number of tags read/set
91 SYM_FUNC_START(mte_copy_tags_to_user)
96 ubfx x4, x4, #MTE_TAG_SHIFT, #MTE_TAG_SIZE
97 user_ldst 2f, sttrb, w4, x0, 0
99 add x1, x1, #MTE_GRANULE_SIZE
103 // exception handling and function return
104 2: sub x0, x0, x3 // update the number of tags copied
106 SYM_FUNC_END(mte_copy_tags_to_user)
109 * Save the tags in a page
113 SYM_FUNC_START(mte_save_page_tags)
114 multitag_transfer_size x7, x5
121 tst x0, #0xFF // 16 tag values fit in a register,
122 b.ne 2b // which is 16*16=256 bytes
126 tst x0, #(PAGE_SIZE - 1)
130 SYM_FUNC_END(mte_save_page_tags)
133 * Restore the tags in a page
137 SYM_FUNC_START(mte_restore_page_tags)
138 multitag_transfer_size x7, x5
147 tst x0, #(PAGE_SIZE - 1)
151 SYM_FUNC_END(mte_restore_page_tags)
154 * Assign allocation tags for a region of memory based on the pointer tag
155 * x0 - source pointer
158 * Note: The address must be non-NULL and MTE_GRANULE_SIZE aligned and
159 * size must be non-zero and MTE_GRANULE_SIZE aligned.
161 SYM_FUNC_START(mte_assign_mem_tag_range)
163 add x0, x0, #MTE_GRANULE_SIZE
164 subs x1, x1, #MTE_GRANULE_SIZE
167 SYM_FUNC_END(mte_assign_mem_tag_range)