blob: 0557af834e036958e1555097beafe55042b1d7b4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Al Virob4b86642016-12-26 04:10:19 -05002#ifndef __ASM_ASM_UACCESS_H
3#define __ASM_ASM_UACCESS_H
4
Will Deacon7cda23d2020-06-30 13:55:59 +01005#include <asm/alternative-macros.h>
Mark Rutland819771c2021-10-19 17:02:13 +01006#include <asm/asm-extable.h>
7#include <asm/assembler.h>
Al Virob4b86642016-12-26 04:10:19 -05008#include <asm/kernel-pgtable.h>
Will Deaconb5195382017-12-01 17:33:48 +00009#include <asm/mmu.h>
Al Virob4b86642016-12-26 04:10:19 -050010#include <asm/sysreg.h>
Al Virob4b86642016-12-26 04:10:19 -050011
12/*
13 * User access enabling/disabling macros.
14 */
15#ifdef CONFIG_ARM64_SW_TTBR0_PAN
16 .macro __uaccess_ttbr0_disable, tmp1
Steve Capper9dfe4822018-01-11 10:11:57 +000017 mrs \tmp1, ttbr1_el1 // swapper_pg_dir
Catalin Marinas6b88a322018-01-10 13:18:30 +000018 bic \tmp1, \tmp1, #TTBR_ASID_MASK
Joey Gouly00ef5432021-02-02 12:36:57 +000019 sub \tmp1, \tmp1, #RESERVED_SWAPPER_OFFSET // reserved_pg_dir
Steve Capper9dfe4822018-01-11 10:11:57 +000020 msr ttbr0_el1, \tmp1 // set reserved TTBR0_EL1
Al Virob4b86642016-12-26 04:10:19 -050021 isb
Joey Gouly00ef5432021-02-02 12:36:57 +000022 add \tmp1, \tmp1, #RESERVED_SWAPPER_OFFSET
Will Deacon27a921e2017-08-10 13:58:16 +010023 msr ttbr1_el1, \tmp1 // set reserved ASID
24 isb
Al Virob4b86642016-12-26 04:10:19 -050025 .endm
26
Will Deacon27a921e2017-08-10 13:58:16 +010027 .macro __uaccess_ttbr0_enable, tmp1, tmp2
Julien Thierry4caf8752019-02-22 09:32:50 +000028 get_current_task \tmp1
Al Virob4b86642016-12-26 04:10:19 -050029 ldr \tmp1, [\tmp1, #TSK_TI_TTBR0] // load saved TTBR0_EL1
Will Deacon27a921e2017-08-10 13:58:16 +010030 mrs \tmp2, ttbr1_el1
31 extr \tmp2, \tmp2, \tmp1, #48
32 ror \tmp2, \tmp2, #16
33 msr ttbr1_el1, \tmp2 // set the active ASID
34 isb
Al Virob4b86642016-12-26 04:10:19 -050035 msr ttbr0_el1, \tmp1 // set the non-PAN TTBR0_EL1
36 isb
37 .endm
38
Catalin Marinas6b88a322018-01-10 13:18:30 +000039 .macro uaccess_ttbr0_disable, tmp1, tmp2
Al Virob4b86642016-12-26 04:10:19 -050040alternative_if_not ARM64_HAS_PAN
Catalin Marinas6b88a322018-01-10 13:18:30 +000041 save_and_disable_irq \tmp2 // avoid preemption
Al Virob4b86642016-12-26 04:10:19 -050042 __uaccess_ttbr0_disable \tmp1
Catalin Marinas6b88a322018-01-10 13:18:30 +000043 restore_irq \tmp2
Al Virob4b86642016-12-26 04:10:19 -050044alternative_else_nop_endif
45 .endm
46
Will Deacon27a921e2017-08-10 13:58:16 +010047 .macro uaccess_ttbr0_enable, tmp1, tmp2, tmp3
Al Virob4b86642016-12-26 04:10:19 -050048alternative_if_not ARM64_HAS_PAN
Will Deacon27a921e2017-08-10 13:58:16 +010049 save_and_disable_irq \tmp3 // avoid preemption
50 __uaccess_ttbr0_enable \tmp1, \tmp2
51 restore_irq \tmp3
Al Virob4b86642016-12-26 04:10:19 -050052alternative_else_nop_endif
53 .endm
54#else
Catalin Marinas6b88a322018-01-10 13:18:30 +000055 .macro uaccess_ttbr0_disable, tmp1, tmp2
Al Virob4b86642016-12-26 04:10:19 -050056 .endm
57
Will Deacon27a921e2017-08-10 13:58:16 +010058 .macro uaccess_ttbr0_enable, tmp1, tmp2, tmp3
Al Virob4b86642016-12-26 04:10:19 -050059 .endm
60#endif
61
Mark Rutland819771c2021-10-19 17:02:13 +010062#define USER(l, x...) \
639999: x; \
64 _asm_extable 9999b, l
65
Mark Rutlande2a21902020-10-26 13:31:47 +000066/*
Mark Rutlandfc703d82020-12-02 13:15:53 +000067 * Generate the assembly for LDTR/STTR with exception table entries.
Mark Rutlande2a21902020-10-26 13:31:47 +000068 * This is complicated as there is no post-increment or pair versions of the
69 * unprivileged instructions, and USER() only works for single instructions.
70 */
Mark Rutland7b90dc42020-12-02 13:15:54 +000071 .macro user_ldp l, reg1, reg2, addr, post_inc
Mark Rutlandfc703d82020-12-02 13:15:53 +0000728888: ldtr \reg1, [\addr];
738889: ldtr \reg2, [\addr, #8];
74 add \addr, \addr, \post_inc;
Mark Rutlande2a21902020-10-26 13:31:47 +000075
76 _asm_extable 8888b,\l;
77 _asm_extable 8889b,\l;
78 .endm
79
Mark Rutland7b90dc42020-12-02 13:15:54 +000080 .macro user_stp l, reg1, reg2, addr, post_inc
Mark Rutlandfc703d82020-12-02 13:15:53 +0000818888: sttr \reg1, [\addr];
828889: sttr \reg2, [\addr, #8];
83 add \addr, \addr, \post_inc;
Mark Rutlande2a21902020-10-26 13:31:47 +000084
85 _asm_extable 8888b,\l;
86 _asm_extable 8889b,\l;
87 .endm
88
Mark Rutland7b90dc42020-12-02 13:15:54 +000089 .macro user_ldst l, inst, reg, addr, post_inc
908888: \inst \reg, [\addr];
Mark Rutlandfc703d82020-12-02 13:15:53 +000091 add \addr, \addr, \post_inc;
Mark Rutlande2a21902020-10-26 13:31:47 +000092
93 _asm_extable 8888b,\l;
94 .endm
Al Virob4b86642016-12-26 04:10:19 -050095#endif