1/*
2 * Copyright (c) 2021, Google Inc. All rights reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include <asm.h>
25#include <arch/asm_macros.h>
26#include <err.h>
27
28/* status_t copy_from_anywhere(void *kdest, user_addr_t usrc, size_t len) */
29FUNCTION(copy_from_anywhere)
30	cbz	x2, .Lcopy_from_anywhere_done
31.Lcopy_from_anywhere_loop:
32	set_fault_handler	.Lcopy_from_anywhere_tryuser
33	ldrb	w9, [x1]
34
35.Lcopy_from_anywhere_continue:
36	add	x1, x1, #1
37	strb	w9, [x0], #1
38	subs	x2, x2, #1
39	b.hi	.Lcopy_from_anywhere_loop
40.Lcopy_from_anywhere_done:
41	mov	x0, #0
42	ret
43
44.Lcopy_from_anywhere_tryuser:
45	set_fault_handler	.Lcopy_from_anywhere_fault
46	ldtrb	w9, [x1]
47	b .Lcopy_from_anywhere_continue
48
49.Lcopy_from_anywhere_fault:
50	strb	wzr, [x0], #1
51	subs	x2, x2, #1
52	b.hi	.Lcopy_from_anywhere_fault
53	mov	x0, #ERR_FAULT
54	ret
55
56/* int tag_for_addr_(vaddr_t addr) */
57FUNCTION(tag_for_addr_)
58.arch_extension memtag
59	set_fault_handler	.Ltag_for_addr__fault
60	ldg	x0, [x0]
61	ubfx	x0, x0, #56, #4
62	ret
63
64.Ltag_for_addr__fault:
65	mov	x0, #ERR_FAULT
66	ret
67