aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKhalid Aziz <khalid.aziz@oracle.com>2018-02-23 17:46:41 -0500
committerDavid S. Miller <davem@davemloft.net>2018-03-18 10:38:48 -0400
commit74a04967482faa7144b93dae3b2e913870dd421c (patch)
tree387f2497cfd053d99504a00ff8ccce0dd5fbd647
parenta4602b62d9fdea41412ba765bbf32ecfc2b6a94c (diff)
sparc64: Add support for ADI (Application Data Integrity)
ADI is a new feature supported on SPARC M7 and newer processors to allow hardware to catch rogue accesses to memory. ADI is supported for data fetches only and not instruction fetches. An app can enable ADI on its data pages, set version tags on them and use versioned addresses to access the data pages. Upper bits of the address contain the version tag. On M7 processors, upper four bits (bits 63-60) contain the version tag. If a rogue app attempts to access ADI enabled data pages, its access is blocked and processor generates an exception. Please see Documentation/sparc/adi.txt for further details. This patch extends mprotect to enable ADI (TSTATE.mcde), enable/disable MCD (Memory Corruption Detection) on selected memory ranges, enable TTE.mcd in PTEs, return ADI parameters to userspace and save/restore ADI version tags on page swap out/in or migration. ADI is not enabled by default for any task. A task must explicitly enable ADI on a memory range and set version tag for ADI to be effective for the task. Signed-off-by: Khalid Aziz <khalid.aziz@oracle.com> Cc: Khalid Aziz <khalid@gonehiking.org> Reviewed-by: Anthony Yznaga <anthony.yznaga@oracle.com> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--Documentation/sparc/adi.txt278
-rw-r--r--arch/sparc/include/asm/mman.h84
-rw-r--r--arch/sparc/include/asm/mmu_64.h17
-rw-r--r--arch/sparc/include/asm/mmu_context_64.h51
-rw-r--r--arch/sparc/include/asm/page_64.h6
-rw-r--r--arch/sparc/include/asm/pgtable_64.h46
-rw-r--r--arch/sparc/include/asm/thread_info_64.h2
-rw-r--r--arch/sparc/include/asm/trap_block.h2
-rw-r--r--arch/sparc/include/uapi/asm/mman.h2
-rw-r--r--arch/sparc/kernel/adi_64.c301
-rw-r--r--arch/sparc/kernel/etrap_64.S27
-rw-r--r--arch/sparc/kernel/process_64.c25
-rw-r--r--arch/sparc/kernel/rtrap_64.S33
-rw-r--r--arch/sparc/kernel/setup_64.c2
-rw-r--r--arch/sparc/kernel/urtt_fill.S7
-rw-r--r--arch/sparc/kernel/vmlinux.lds.S5
-rw-r--r--arch/sparc/mm/gup.c37
-rw-r--r--arch/sparc/mm/hugetlbpage.c14
-rw-r--r--arch/sparc/mm/init_64.c69
-rw-r--r--arch/sparc/mm/tsb.c21
-rw-r--r--include/linux/mm.h3
-rw-r--r--mm/ksm.c4
22 files changed, 1028 insertions, 8 deletions
diff --git a/Documentation/sparc/adi.txt b/Documentation/sparc/adi.txt
new file mode 100644
index 000000000000..e1aed155fb89
--- /dev/null
+++ b/Documentation/sparc/adi.txt
@@ -0,0 +1,278 @@
1Application Data Integrity (ADI)
2================================
3
4SPARC M7 processor adds the Application Data Integrity (ADI) feature.
5ADI allows a task to set version tags on any subset of its address
6space. Once ADI is enabled and version tags are set for ranges of
7address space of a task, the processor will compare the tag in pointers
8to memory in these ranges to the version set by the application
9previously. Access to memory is granted only if the tag in given pointer
10matches the tag set by the application. In case of mismatch, processor
11raises an exception.
12
13Following steps must be taken by a task to enable ADI fully:
14
151. Set the user mode PSTATE.mcde bit. This acts as master switch for
16 the task's entire address space to enable/disable ADI for the task.
17
182. Set TTE.mcd bit on any TLB entries that correspond to the range of
19 addresses ADI is being enabled on. MMU checks the version tag only
20 on the pages that have TTE.mcd bit set.
21
223. Set the version tag for virtual addresses using stxa instruction
23 and one of the MCD specific ASIs. Each stxa instruction sets the
24 given tag for one ADI block size number of bytes. This step must
25 be repeated for entire page to set tags for entire page.
26
27ADI block size for the platform is provided by the hypervisor to kernel
28in machine description tables. Hypervisor also provides the number of
29top bits in the virtual address that specify the version tag. Once
30version tag has been set for a memory location, the tag is stored in the
31physical memory and the same tag must be present in the ADI version tag
32bits of the virtual address being presented to the MMU. For example on
33SPARC M7 processor, MMU uses bits 63-60 for version tags and ADI block
34size is same as cacheline size which is 64 bytes. A task that sets ADI
35version to, say 10, on a range of memory, must access that memory using
36virtual addresses that contain 0xa in bits 63-60.
37
38ADI is enabled on a set of pages using mprotect() with PROT_ADI flag.
39When ADI is enabled on a set of pages by a task for the first time,
40kernel sets the PSTATE.mcde bit fot the task. Version tags for memory
41addresses are set with an stxa instruction on the addresses using
42ASI_MCD_PRIMARY or ASI_MCD_ST_BLKINIT_PRIMARY. ADI block size is
43provided by the hypervisor to the kernel. Kernel returns the value of
44ADI block size to userspace using auxiliary vector along with other ADI
45info. Following auxiliary vectors are provided by the kernel:
46
47 AT_ADI_BLKSZ ADI block size. This is the granularity and
48 alignment, in bytes, of ADI versioning.
49 AT_ADI_NBITS Number of ADI version bits in the VA
50
51
52IMPORTANT NOTES:
53
54- Version tag values of 0x0 and 0xf are reserved. These values match any
55 tag in virtual address and never generate a mismatch exception.
56
57- Version tags are set on virtual addresses from userspace even though
58 tags are stored in physical memory. Tags are set on a physical page
59 after it has been allocated to a task and a pte has been created for
60 it.
61
62- When a task frees a memory page it had set version tags on, the page
63 goes back to free page pool. When this page is re-allocated to a task,
64 kernel clears the page using block initialization ASI which clears the
65 version tags as well for the page. If a page allocated to a task is
66 freed and allocated back to the same task, old version tags set by the
67 task on that page will no longer be present.
68
69- ADI tag mismatches are not detected for non-faulting loads.
70
71- Kernel does not set any tags for user pages and it is entirely a
72 task's responsibility to set any version tags. Kernel does ensure the
73 version tags are preserved if a page is swapped out to the disk and
74 swapped back in. It also preserves that version tags if a page is
75 migrated.
76
77- ADI works for any size pages. A userspace task need not be aware of
78 page size when using ADI. It can simply select a virtual address
79 range, enable ADI on the range using mprotect() and set version tags
80 for the entire range. mprotect() ensures range is aligned to page size
81 and is a multiple of page size.
82
83- ADI tags can only be set on writable memory. For example, ADI tags can
84 not be set on read-only mappings.
85
86
87
88ADI related traps
89-----------------
90
91With ADI enabled, following new traps may occur:
92
93Disrupting memory corruption
94
95 When a store accesses a memory localtion that has TTE.mcd=1,
96 the task is running with ADI enabled (PSTATE.mcde=1), and the ADI
97 tag in the address used (bits 63:60) does not match the tag set on
98 the corresponding cacheline, a memory corruption trap occurs. By
99 default, it is a disrupting trap and is sent to the hypervisor
100 first. Hypervisor creates a sun4v error report and sends a
101 resumable error (TT=0x7e) trap to the kernel. The kernel sends
102 a SIGSEGV to the task that resulted in this trap with the following
103 info:
104
105 siginfo.si_signo = SIGSEGV;
106 siginfo.errno = 0;
107 siginfo.si_code = SEGV_ADIDERR;
108 siginfo.si_addr = addr; /* PC where first mismatch occurred */
109 siginfo.si_trapno = 0;
110
111
112Precise memory corruption
113
114 When a store accesses a memory location that has TTE.mcd=1,
115 the task is running with ADI enabled (PSTATE.mcde=1), and the ADI
116 tag in the address used (bits 63:60) does not match the tag set on
117 the corresponding cacheline, a memory corruption trap occurs. If
118 MCD precise exception is enabled (MCDPERR=1), a precise
119 exception is sent to the kernel with TT=0x1a. The kernel sends
120 a SIGSEGV to the task that resulted in this trap with the following
121 info:
122
123 siginfo.si_signo = SIGSEGV;
124 siginfo.errno = 0;
125 siginfo.si_code = SEGV_ADIPERR;
126 siginfo.si_addr = addr; /* address that caused trap */
127 siginfo.si_trapno = 0;
128
129 NOTE: ADI tag mismatch on a load always results in precise trap.
130
131
132MCD disabled
133
134 When a task has not enabled ADI and attempts to set ADI version
135 on a memory address, processor sends an MCD disabled trap. This
136 trap is handled by hypervisor first and the hypervisor vectors this
137 trap through to the kernel as Data Access Exception trap with
138 fault type set to 0xa (invalid ASI). When this occurs, the kernel
139 sends the task SIGSEGV signal with following info:
140
141 siginfo.si_signo = SIGSEGV;
142 siginfo.errno = 0;
143 siginfo.si_code = SEGV_ACCADI;
144 siginfo.si_addr = addr; /* address that caused trap */
145 siginfo.si_trapno = 0;
146
147
148Sample program to use ADI
149-------------------------
150
151Following sample program is meant to illustrate how to use the ADI
152functionality.
153
154#include <unistd.h>
155#include <stdio.h>
156#include <stdlib.h>
157#include <elf.h>
158#include <sys/ipc.h>
159#include <sys/shm.h>
160#include <sys/mman.h>
161#include <asm/asi.h>
162
163#ifndef AT_ADI_BLKSZ
164#define AT_ADI_BLKSZ 48
165#endif
166#ifndef AT_ADI_NBITS
167#define AT_ADI_NBITS 49
168#endif
169
170#ifndef PROT_ADI
171#define PROT_ADI 0x10
172#endif
173
174#define BUFFER_SIZE 32*1024*1024UL
175
176main(int argc, char* argv[], char* envp[])
177{
178 unsigned long i, mcde, adi_blksz, adi_nbits;
179 char *shmaddr, *tmp_addr, *end, *veraddr, *clraddr;
180 int shmid, version;
181 Elf64_auxv_t *auxv;
182
183 adi_blksz = 0;
184
185 while(*envp++ != NULL);
186 for (auxv = (Elf64_auxv_t *)envp; auxv->a_type != AT_NULL; auxv++) {
187 switch (auxv->a_type) {
188 case AT_ADI_BLKSZ:
189 adi_blksz = auxv->a_un.a_val;
190 break;
191 case AT_ADI_NBITS:
192 adi_nbits = auxv->a_un.a_val;
193 break;
194 }
195 }
196 if (adi_blksz == 0) {
197 fprintf(stderr, "Oops! ADI is not supported\n");
198 exit(1);
199 }
200
201 printf("ADI capabilities:\n");
202 printf("\tBlock size = %ld\n", adi_blksz);
203 printf("\tNumber of bits = %ld\n", adi_nbits);
204
205 if ((shmid = shmget(2, BUFFER_SIZE,
206 IPC_CREAT | SHM_R | SHM_W)) < 0) {
207 perror("shmget failed");
208 exit(1);
209 }
210
211 shmaddr = shmat(shmid, NULL, 0);
212 if (shmaddr == (char *)-1) {
213 perror("shm attach failed");
214 shmctl(shmid, IPC_RMID, NULL);
215 exit(1);
216 }
217
218 if (mprotect(shmaddr, BUFFER_SIZE, PROT_READ|PROT_WRITE|PROT_ADI)) {
219 perror("mprotect failed");
220 goto err_out;
221 }
222
223 /* Set the ADI version tag on the shm segment
224 */
225 version = 10;
226 tmp_addr = shmaddr;
227 end = shmaddr + BUFFER_SIZE;
228 while (tmp_addr < end) {
229 asm volatile(
230 "stxa %1, [%0]0x90\n\t"
231 :
232 : "r" (tmp_addr), "r" (version));
233 tmp_addr += adi_blksz;
234 }
235 asm volatile("membar #Sync\n\t");
236
237 /* Create a versioned address from the normal address by placing
238 * version tag in the upper adi_nbits bits
239 */
240 tmp_addr = (void *) ((unsigned long)shmaddr << adi_nbits);
241 tmp_addr = (void *) ((unsigned long)tmp_addr >> adi_nbits);
242 veraddr = (void *) (((unsigned long)version << (64-adi_nbits))
243 | (unsigned long)tmp_addr);
244
245 printf("Starting the writes:\n");
246 for (i = 0; i < BUFFER_SIZE; i++) {
247 veraddr[i] = (char)(i);
248 if (!(i % (1024 * 1024)))
249 printf(".");
250 }
251 printf("\n");
252
253 printf("Verifying data...");
254 fflush(stdout);
255 for (i = 0; i < BUFFER_SIZE; i++)
256 if (veraddr[i] != (char)i)
257 printf("\nIndex %lu mismatched\n", i);
258 printf("Done.\n");
259
260 /* Disable ADI and clean up
261 */
262 if (mprotect(shmaddr, BUFFER_SIZE, PROT_READ|PROT_WRITE)) {
263 perror("mprotect failed");
264 goto err_out;
265 }
266
267 if (shmdt((const void *)shmaddr) != 0)
268 perror("Detach failure");
269 shmctl(shmid, IPC_RMID, NULL);
270
271 exit(0);
272
273err_out:
274 if (shmdt((const void *)shmaddr) != 0)
275 perror("Detach failure");
276 shmctl(shmid, IPC_RMID, NULL);
277 exit(1);
278}
diff --git a/arch/sparc/include/asm/mman.h b/arch/sparc/include/asm/mman.h
index 7e9472143f9b..f94532f25db1 100644
--- a/arch/sparc/include/asm/mman.h
+++ b/arch/sparc/include/asm/mman.h
@@ -7,5 +7,87 @@
7#ifndef __ASSEMBLY__ 7#ifndef __ASSEMBLY__
8#define arch_mmap_check(addr,len,flags) sparc_mmap_check(addr,len) 8#define arch_mmap_check(addr,len,flags) sparc_mmap_check(addr,len)
9int sparc_mmap_check(unsigned long addr, unsigned long len); 9int sparc_mmap_check(unsigned long addr, unsigned long len);
10#endif 10
11#ifdef CONFIG_SPARC64
12#include <asm/adi_64.h>
13
14static inline void ipi_set_tstate_mcde(void *arg)
15{
16 struct mm_struct *mm = arg;
17
18 /* Set TSTATE_MCDE for the task using address map that ADI has been
19 * enabled on if the task is running. If not, it will be set
20 * automatically at the next context switch
21 */
22 if (current->mm == mm) {
23 struct pt_regs *regs;
24
25 regs = task_pt_regs(current);
26 regs->tstate |= TSTATE_MCDE;
27 }
28}
29
30#define arch_calc_vm_prot_bits(prot, pkey) sparc_calc_vm_prot_bits(prot)
31static inline unsigned long sparc_calc_vm_prot_bits(unsigned long prot)
32{
33 if (adi_capable() && (prot & PROT_ADI)) {
34 struct pt_regs *regs;
35
36 if (!current->mm->context.adi) {
37 regs = task_pt_regs(current);
38 regs->tstate |= TSTATE_MCDE;
39 current->mm->context.adi = true;
40 on_each_cpu_mask(mm_cpumask(current->mm),
41 ipi_set_tstate_mcde, current->mm, 0);
42 }
43 return VM_SPARC_ADI;
44 } else {
45 return 0;
46 }
47}
48
49#define arch_vm_get_page_prot(vm_flags) sparc_vm_get_page_prot(vm_flags)
50static inline pgprot_t sparc_vm_get_page_prot(unsigned long vm_flags)
51{
52 return (vm_flags & VM_SPARC_ADI) ? __pgprot(_PAGE_MCD_4V) : __pgprot(0);
53}
54
55#define arch_validate_prot(prot, addr) sparc_validate_prot(prot, addr)
56static inline int sparc_validate_prot(unsigned long prot, unsigned long addr)
57{
58 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM | PROT_ADI))
59 return 0;
60 if (prot & PROT_ADI) {
61 if (!adi_capable())
62 return 0;
63
64 if (addr) {
65 struct vm_area_struct *vma;
66
67 vma = find_vma(current->mm, addr);
68 if (vma) {
69 /* ADI can not be enabled on PFN
70 * mapped pages
71 */
72 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
73 return 0;
74
75 /* Mergeable pages can become unmergeable
76 * if ADI is enabled on them even if they
77 * have identical data on them. This can be
78 * because ADI enabled pages with identical
79 * data may still not have identical ADI
80 * tags on them. Disallow ADI on mergeable
81 * pages.
82 */
83 if (vma->vm_flags & VM_MERGEABLE)
84 return 0;
85 }
86 }
87 }
88 return 1;
89}
90#endif /* CONFIG_SPARC64 */
91
92#endif /* __ASSEMBLY__ */
11#endif /* __SPARC_MMAN_H__ */ 93#endif /* __SPARC_MMAN_H__ */
diff --git a/arch/sparc/include/asm/mmu_64.h b/arch/sparc/include/asm/mmu_64.h
index ad4fb93508ba..7e2704c770e9 100644
--- a/arch/sparc/include/asm/mmu_64.h
+++ b/arch/sparc/include/asm/mmu_64.h
@@ -90,6 +90,20 @@ struct tsb_config {
90#define MM_NUM_TSBS 1 90#define MM_NUM_TSBS 1
91#endif 91#endif
92 92
93/* ADI tags are stored when a page is swapped out and the storage for
94 * tags is allocated dynamically. There is a tag storage descriptor
95 * associated with each set of tag storage pages. Tag storage descriptors
96 * are allocated dynamically. Since kernel will allocate a full page for
97 * each tag storage descriptor, we can store up to
98 * PAGE_SIZE/sizeof(tag storage descriptor) descriptors on that page.
99 */
100typedef struct {
101 unsigned long start; /* Start address for this tag storage */
102 unsigned long end; /* Last address for tag storage */
103 unsigned char *tags; /* Where the tags are */
104 unsigned long tag_users; /* number of references to descriptor */
105} tag_storage_desc_t;
106
93typedef struct { 107typedef struct {
94 spinlock_t lock; 108 spinlock_t lock;
95 unsigned long sparc64_ctx_val; 109 unsigned long sparc64_ctx_val;
@@ -98,6 +112,9 @@ typedef struct {
98 struct tsb_config tsb_block[MM_NUM_TSBS]; 112 struct tsb_config tsb_block[MM_NUM_TSBS];
99 struct hv_tsb_descr tsb_descr[MM_NUM_TSBS]; 113 struct hv_tsb_descr tsb_descr[MM_NUM_TSBS];
100 void *vdso; 114 void *vdso;
115 bool adi;
116 tag_storage_desc_t *tag_store;
117 spinlock_t tag_lock;
101} mm_context_t; 118} mm_context_t;
102 119
103#endif /* !__ASSEMBLY__ */ 120#endif /* !__ASSEMBLY__ */
diff --git a/arch/sparc/include/asm/mmu_context_64.h b/arch/sparc/include/asm/mmu_context_64.h
index b361702ef52a..312fcee8df2b 100644
--- a/arch/sparc/include/asm/mmu_context_64.h
+++ b/arch/sparc/include/asm/mmu_context_64.h
@@ -9,8 +9,10 @@
9#include <linux/spinlock.h> 9#include <linux/spinlock.h>
10#include <linux/mm_types.h> 10#include <linux/mm_types.h>
11#include <linux/smp.h> 11#include <linux/smp.h>
12#include <linux/sched.h>
12 13
13#include <asm/spitfire.h> 14#include <asm/spitfire.h>
15#include <asm/adi_64.h>
14#include <asm-generic/mm_hooks.h> 16#include <asm-generic/mm_hooks.h>
15#include <asm/percpu.h> 17#include <asm/percpu.h>
16 18
@@ -136,6 +138,55 @@ static inline void switch_mm(struct mm_struct *old_mm, struct mm_struct *mm, str
136 138
137#define deactivate_mm(tsk,mm) do { } while (0) 139#define deactivate_mm(tsk,mm) do { } while (0)
138#define activate_mm(active_mm, mm) switch_mm(active_mm, mm, NULL) 140#define activate_mm(active_mm, mm) switch_mm(active_mm, mm, NULL)
141
142#define __HAVE_ARCH_START_CONTEXT_SWITCH
143static inline void arch_start_context_switch(struct task_struct *prev)
144{
145 /* Save the current state of MCDPER register for the process
146 * we are switching from
147 */
148 if (adi_capable()) {
149 register unsigned long tmp_mcdper;
150
151 __asm__ __volatile__(
152 ".word 0x83438000\n\t" /* rd %mcdper, %g1 */
153 "mov %%g1, %0\n\t"
154 : "=r" (tmp_mcdper)
155 :
156 : "g1");
157 if (tmp_mcdper)
158 set_tsk_thread_flag(prev, TIF_MCDPER);
159 else
160 clear_tsk_thread_flag(prev, TIF_MCDPER);
161 }
162}
163
164#define finish_arch_post_lock_switch finish_arch_post_lock_switch
165static inline void finish_arch_post_lock_switch(void)
166{
167 /* Restore the state of MCDPER register for the new process
168 * just switched to.
169 */
170 if (adi_capable()) {
171 register unsigned long tmp_mcdper;
172
173 tmp_mcdper = test_thread_flag(TIF_MCDPER);
174 __asm__ __volatile__(
175 "mov %0, %%g1\n\t"
176 ".word 0x9d800001\n\t" /* wr %g0, %g1, %mcdper" */
177 ".word 0xaf902001\n\t" /* wrpr %g0, 1, %pmcdper */
178 :
179 : "ir" (tmp_mcdper)
180 : "g1");
181 if (current && current->mm && current->mm->context.adi) {
182 struct pt_regs *regs;
183
184 regs = task_pt_regs(current);
185 regs->tstate |= TSTATE_MCDE;
186 }
187 }
188}
189
139#endif /* !(__ASSEMBLY__) */ 190#endif /* !(__ASSEMBLY__) */
140 191
141#endif /* !(__SPARC64_MMU_CONTEXT_H) */ 192#endif /* !(__SPARC64_MMU_CONTEXT_H) */
diff --git a/arch/sparc/include/asm/page_64.h b/arch/sparc/include/asm/page_64.h
index c28379b1b0fc..e80f2d5bf62f 100644
--- a/arch/sparc/include/asm/page_64.h
+++ b/arch/sparc/include/asm/page_64.h
@@ -48,6 +48,12 @@ struct page;
48void clear_user_page(void *addr, unsigned long vaddr, struct page *page); 48void clear_user_page(void *addr, unsigned long vaddr, struct page *page);
49#define copy_page(X,Y) memcpy((void *)(X), (void *)(Y), PAGE_SIZE) 49#define copy_page(X,Y) memcpy((void *)(X), (void *)(Y), PAGE_SIZE)
50void copy_user_page(void *to, void *from, unsigned long vaddr, struct page *topage); 50void copy_user_page(void *to, void *from, unsigned long vaddr, struct page *topage);
51#define __HAVE_ARCH_COPY_USER_HIGHPAGE
52struct vm_area_struct;
53void copy_user_highpage(struct page *to, struct page *from,
54 unsigned long vaddr, struct vm_area_struct *vma);
55#define __HAVE_ARCH_COPY_HIGHPAGE
56void copy_highpage(struct page *to, struct page *from);
51 57
52/* Unlike sparc32, sparc64's parameter passing API is more 58/* Unlike sparc32, sparc64's parameter passing API is more
53 * sane in that structures which as small enough are passed 59 * sane in that structures which as small enough are passed
diff --git a/arch/sparc/include/asm/pgtable_64.h b/arch/sparc/include/asm/pgtable_64.h
index 619332a44402..44d6ac47e035 100644
--- a/arch/sparc/include/asm/pgtable_64.h
+++ b/arch/sparc/include/asm/pgtable_64.h
@@ -19,6 +19,7 @@
19#include <asm/types.h> 19#include <asm/types.h>
20#include <asm/spitfire.h> 20#include <asm/spitfire.h>
21#include <asm/asi.h> 21#include <asm/asi.h>
22#include <asm/adi.h>
22#include <asm/page.h> 23#include <asm/page.h>
23#include <asm/processor.h> 24#include <asm/processor.h>
24 25
@@ -606,6 +607,18 @@ static inline pte_t pte_mkspecial(pte_t pte)
606 return pte; 607 return pte;
607} 608}
608 609
610static inline pte_t pte_mkmcd(pte_t pte)
611{
612 pte_val(pte) |= _PAGE_MCD_4V;
613 return pte;
614}
615
616static inline pte_t pte_mknotmcd(pte_t pte)
617{
618 pte_val(pte) &= ~_PAGE_MCD_4V;
619 return pte;
620}
621
609static inline unsigned long pte_young(pte_t pte) 622static inline unsigned long pte_young(pte_t pte)
610{ 623{
611 unsigned long mask; 624 unsigned long mask;
@@ -1048,6 +1061,39 @@ int page_in_phys_avail(unsigned long paddr);
1048int remap_pfn_range(struct vm_area_struct *, unsigned long, unsigned long, 1061int remap_pfn_range(struct vm_area_struct *, unsigned long, unsigned long,
1049 unsigned long, pgprot_t); 1062 unsigned long, pgprot_t);
1050 1063
1064void adi_restore_tags(struct mm_struct *mm, struct vm_area_struct *vma,
1065 unsigned long addr, pte_t pte);
1066
1067int adi_save_tags(struct mm_struct *mm, struct vm_area_struct *vma,
1068 unsigned long addr, pte_t oldpte);
1069
1070#define __HAVE_ARCH_DO_SWAP_PAGE
1071static inline void arch_do_swap_page(struct mm_struct *mm,
1072 struct vm_area_struct *vma,
1073 unsigned long addr,
1074 pte_t pte, pte_t oldpte)
1075{
1076 /* If this is a new page being mapped in, there can be no
1077 * ADI tags stored away for this page. Skip looking for
1078 * stored tags
1079 */
1080 if (pte_none(oldpte))
1081 return;
1082
1083 if (adi_state.enabled && (pte_val(pte) & _PAGE_MCD_4V))
1084 adi_restore_tags(mm, vma, addr, pte);
1085}
1086
1087#define __HAVE_ARCH_UNMAP_ONE
1088static inline int arch_unmap_one(struct mm_struct *mm,
1089 struct vm_area_struct *vma,
1090 unsigned long addr, pte_t oldpte)
1091{
1092 if (adi_state.enabled && (pte_val(oldpte) & _PAGE_MCD_4V))
1093 return adi_save_tags(mm, vma, addr, oldpte);
1094 return 0;
1095}
1096
1051static inline int io_remap_pfn_range(struct vm_area_struct *vma, 1097static inline int io_remap_pfn_range(struct vm_area_struct *vma,
1052 unsigned long from, unsigned long pfn, 1098 unsigned long from, unsigned long pfn,
1053 unsigned long size, pgprot_t prot) 1099 unsigned long size, pgprot_t prot)
diff --git a/arch/sparc/include/asm/thread_info_64.h b/arch/sparc/include/asm/thread_info_64.h
index f7e7b0baec9f..7fb676360928 100644
--- a/arch/sparc/include/asm/thread_info_64.h
+++ b/arch/sparc/include/asm/thread_info_64.h
@@ -188,7 +188,7 @@ register struct thread_info *current_thread_info_reg asm("g6");
188 * in using in assembly, else we can't use the mask as 188 * in using in assembly, else we can't use the mask as
189 * an immediate value in instructions such as andcc. 189 * an immediate value in instructions such as andcc.
190 */ 190 */
191/* flag bit 12 is available */ 191#define TIF_MCDPER 12 /* Precise MCD exception */
192#define TIF_MEMDIE 13 /* is terminating due to OOM killer */ 192#define TIF_MEMDIE 13 /* is terminating due to OOM killer */
193#define TIF_POLLING_NRFLAG 14 193#define TIF_POLLING_NRFLAG 14
194 194
diff --git a/arch/sparc/include/asm/trap_block.h b/arch/sparc/include/asm/trap_block.h
index 6a4c8652ad67..0f6d0c4f6683 100644
--- a/arch/sparc/include/asm/trap_block.h
+++ b/arch/sparc/include/asm/trap_block.h
@@ -76,6 +76,8 @@ extern struct sun4v_1insn_patch_entry __sun4v_1insn_patch,
76 __sun4v_1insn_patch_end; 76 __sun4v_1insn_patch_end;
77extern struct sun4v_1insn_patch_entry __fast_win_ctrl_1insn_patch, 77extern struct sun4v_1insn_patch_entry __fast_win_ctrl_1insn_patch,
78 __fast_win_ctrl_1insn_patch_end; 78 __fast_win_ctrl_1insn_patch_end;
79extern struct sun4v_1insn_patch_entry __sun_m7_1insn_patch,
80 __sun_m7_1insn_patch_end;
79 81
80struct sun4v_2insn_patch_entry { 82struct sun4v_2insn_patch_entry {
81 unsigned int addr; 83 unsigned int addr;
diff --git a/arch/sparc/include/uapi/asm/mman.h b/arch/sparc/include/uapi/asm/mman.h
index 715a2c927e79..f6f99ec65bb3 100644
--- a/arch/sparc/include/uapi/asm/mman.h
+++ b/arch/sparc/include/uapi/asm/mman.h
@@ -6,6 +6,8 @@
6 6
7/* SunOS'ified... */ 7/* SunOS'ified... */
8 8
9#define PROT_ADI 0x10 /* ADI enabled */
10
9#define MAP_RENAME MAP_ANONYMOUS /* In SunOS terminology */ 11#define MAP_RENAME MAP_ANONYMOUS /* In SunOS terminology */
10#define MAP_NORESERVE 0x40 /* don't reserve swap pages */ 12#define MAP_NORESERVE 0x40 /* don't reserve swap pages */
11#define MAP_INHERIT 0x80 /* SunOS doesn't do this, but... */ 13#define MAP_INHERIT 0x80 /* SunOS doesn't do this, but... */
diff --git a/arch/sparc/kernel/adi_64.c b/arch/sparc/kernel/adi_64.c
index 8fb72585d9f1..d0a2ac975b42 100644
--- a/arch/sparc/kernel/adi_64.c
+++ b/arch/sparc/kernel/adi_64.c
@@ -8,10 +8,24 @@
8 * This work is licensed under the terms of the GNU GPL, version 2. 8 * This work is licensed under the terms of the GNU GPL, version 2.
9 */ 9 */
10#include <linux/init.h> 10#include <linux/init.h>
11#include <linux/slab.h>
12#include <linux/mm_types.h>
11#include <asm/mdesc.h> 13#include <asm/mdesc.h>
12#include <asm/adi_64.h> 14#include <asm/adi_64.h>
15#include <asm/mmu_64.h>
16#include <asm/pgtable_64.h>
17
18/* Each page of storage for ADI tags can accommodate tags for 128
19 * pages. When ADI enabled pages are being swapped out, it would be
20 * prudent to allocate at least enough tag storage space to accommodate
21 * SWAPFILE_CLUSTER number of pages. Allocate enough tag storage to
22 * store tags for four SWAPFILE_CLUSTER pages to reduce need for
23 * further allocations for same vma.
24 */
25#define TAG_STORAGE_PAGES 8
13 26
14struct adi_config adi_state; 27struct adi_config adi_state;
28EXPORT_SYMBOL(adi_state);
15 29
16/* mdesc_adi_init() : Parse machine description provided by the 30/* mdesc_adi_init() : Parse machine description provided by the
17 * hypervisor to detect ADI capabilities 31 * hypervisor to detect ADI capabilities
@@ -84,6 +98,19 @@ void __init mdesc_adi_init(void)
84 goto adi_not_found; 98 goto adi_not_found;
85 adi_state.caps.ue_on_adi = *val; 99 adi_state.caps.ue_on_adi = *val;
86 100
101 /* Some of the code to support swapping ADI tags is written
102 * assumption that two ADI tags can fit inside one byte. If
103 * this assumption is broken by a future architecture change,
104 * that code will have to be revisited. If that were to happen,
105 * disable ADI support so we do not get unpredictable results
106 * with programs trying to use ADI and their pages getting
107 * swapped out
108 */
109 if (adi_state.caps.nbits > 4) {
110 pr_warn("WARNING: ADI tag size >4 on this platform. Disabling AADI support\n");
111 adi_state.enabled = false;
112 }
113
87 mdesc_release(hp); 114 mdesc_release(hp);
88 return; 115 return;
89 116
@@ -94,3 +121,277 @@ adi_not_found:
94 if (hp) 121 if (hp)
95 mdesc_release(hp); 122 mdesc_release(hp);
96} 123}
124
125tag_storage_desc_t *find_tag_store(struct mm_struct *mm,
126 struct vm_area_struct *vma,
127 unsigned long addr)
128{
129 tag_storage_desc_t *tag_desc = NULL;
130 unsigned long i, max_desc, flags;
131
132 /* Check if this vma already has tag storage descriptor
133 * allocated for it.
134 */
135 max_desc = PAGE_SIZE/sizeof(tag_storage_desc_t);
136 if (mm->context.tag_store) {
137 tag_desc = mm->context.tag_store;
138 spin_lock_irqsave(&mm->context.tag_lock, flags);
139 for (i = 0; i < max_desc; i++) {
140 if ((addr >= tag_desc->start) &&
141 ((addr + PAGE_SIZE - 1) <= tag_desc->end))
142 break;
143 tag_desc++;
144 }
145 spin_unlock_irqrestore(&mm->context.tag_lock, flags);
146
147 /* If no matching entries were found, this must be a
148 * freshly allocated page
149 */
150 if (i >= max_desc)
151 tag_desc = NULL;
152 }
153
154 return tag_desc;
155}
156
157tag_storage_desc_t *alloc_tag_store(struct mm_struct *mm,
158 struct vm_area_struct *vma,
159 unsigned long addr)
160{
161 unsigned char *tags;
162 unsigned long i, size, max_desc, flags;
163 tag_storage_desc_t *tag_desc, *open_desc;
164 unsigned long end_addr, hole_start, hole_end;
165
166 max_desc = PAGE_SIZE/sizeof(tag_storage_desc_t);
167 open_desc = NULL;
168 hole_start = 0;
169 hole_end = ULONG_MAX;
170 end_addr = addr + PAGE_SIZE - 1;
171
172 /* Check if this vma already has tag storage descriptor
173 * allocated for it.
174 */
175 spin_lock_irqsave(&mm->context.tag_lock, flags);
176 if (mm->context.tag_store) {
177 tag_desc = mm->context.tag_store;
178
179 /* Look for a matching entry for this address. While doing
180 * that, look for the first open slot as well and find
181 * the hole in already allocated range where this request
182 * will fit in.
183 */
184 for (i = 0; i < max_desc; i++) {
185 if (tag_desc->tag_users == 0) {
186 if (open_desc == NULL)
187 open_desc = tag_desc;
188 } else {
189 if ((addr >= tag_desc->start) &&
190 (tag_desc->end >= (addr + PAGE_SIZE - 1))) {
191 tag_desc->tag_users++;
192 goto out;
193 }
194 }
195 if ((tag_desc->start > end_addr) &&
196 (tag_desc->start < hole_end))
197 hole_end = tag_desc->start;
198 if ((tag_desc->end < addr) &&
199 (tag_desc->end > hole_start))
200 hole_start = tag_desc->end;
201 tag_desc++;
202 }
203
204 } else {
205 size = sizeof(tag_storage_desc_t)*max_desc;
206 mm->context.tag_store = kzalloc(size, GFP_NOWAIT|__GFP_NOWARN);
207 if (mm->context.tag_store == NULL) {
208 tag_desc = NULL;
209 goto out;
210 }
211 tag_desc = mm->context.tag_store;
212 for (i = 0; i < max_desc; i++, tag_desc++)
213 tag_desc->tag_users = 0;
214 open_desc = mm->context.tag_store;
215 i = 0;
216 }
217
218 /* Check if we ran out of tag storage descriptors */
219 if (open_desc == NULL) {
220 tag_desc = NULL;
221 goto out;
222 }
223
224 /* Mark this tag descriptor slot in use and then initialize it */
225 tag_desc = open_desc;
226 tag_desc->tag_users = 1;
227
228 /* Tag storage has not been allocated for this vma and space
229 * is available in tag storage descriptor. Since this page is
230 * being swapped out, there is high probability subsequent pages
231 * in the VMA will be swapped out as well. Allocate pages to
232 * store tags for as many pages in this vma as possible but not
233 * more than TAG_STORAGE_PAGES. Each byte in tag space holds
234 * two ADI tags since each ADI tag is 4 bits. Each ADI tag
235 * covers adi_blksize() worth of addresses. Check if the hole is
236 * big enough to accommodate full address range for using
237 * TAG_STORAGE_PAGES number of tag pages.
238 */
239 size = TAG_STORAGE_PAGES * PAGE_SIZE;
240 end_addr = addr + (size*2*adi_blksize()) - 1;
241 /* Check for overflow. If overflow occurs, allocate only one page */
242 if (end_addr < addr) {
243 size = PAGE_SIZE;
244 end_addr = addr + (size*2*adi_blksize()) - 1;
245 /* If overflow happens with the minimum tag storage
246 * allocation as well, adjust ending address for this
247 * tag storage.
248 */
249 if (end_addr < addr)
250 end_addr = ULONG_MAX;
251 }
252 if (hole_end < end_addr) {
253 /* Available hole is too small on the upper end of
254 * address. Can we expand the range towards the lower
255 * address and maximize use of this slot?
256 */
257 unsigned long tmp_addr;
258
259 end_addr = hole_end - 1;
260 tmp_addr = end_addr - (size*2*adi_blksize()) + 1;
261 /* Check for underflow. If underflow occurs, allocate
262 * only one page for storing ADI tags
263 */
264 if (tmp_addr > addr) {
265 size = PAGE_SIZE;
266 tmp_addr = end_addr - (size*2*adi_blksize()) - 1;
267 /* If underflow happens with the minimum tag storage
268 * allocation as well, adjust starting address for
269 * this tag storage.
270 */
271 if (tmp_addr > addr)
272 tmp_addr = 0;
273 }
274 if (tmp_addr < hole_start) {
275 /* Available hole is restricted on lower address
276 * end as well
277 */
278 tmp_addr = hole_start + 1;
279 }
280 addr = tmp_addr;
281 size = (end_addr + 1 - addr)/(2*adi_blksize());
282 size = (size + (PAGE_SIZE-adi_blksize()))/PAGE_SIZE;
283 size = size * PAGE_SIZE;
284 }
285 tags = kzalloc(size, GFP_NOWAIT|__GFP_NOWARN);
286 if (tags == NULL) {
287 tag_desc->tag_users = 0;
288 tag_desc = NULL;
289 goto out;
290 }
291 tag_desc->start = addr;
292 tag_desc->tags = tags;
293 tag_desc->end = end_addr;
294
295out:
296 spin_unlock_irqrestore(&mm->context.tag_lock, flags);
297 return tag_desc;
298}
299
300void del_tag_store(tag_storage_desc_t *tag_desc, struct mm_struct *mm)
301{
302 unsigned long flags;
303 unsigned char *tags = NULL;
304
305 spin_lock_irqsave(&mm->context.tag_lock, flags);
306 tag_desc->tag_users--;
307 if (tag_desc->tag_users == 0) {
308 tag_desc->start = tag_desc->end = 0;
309 /* Do not free up the tag storage space allocated
310 * by the first descriptor. This is persistent
311 * emergency tag storage space for the task.
312 */
313 if (tag_desc != mm->context.tag_store) {
314 tags = tag_desc->tags;
315 tag_desc->tags = NULL;
316 }
317 }
318 spin_unlock_irqrestore(&mm->context.tag_lock, flags);
319 kfree(tags);
320}
321
322#define tag_start(addr, tag_desc) \
323 ((tag_desc)->tags + ((addr - (tag_desc)->start)/(2*adi_blksize())))
324
325/* Retrieve any saved ADI tags for the page being swapped back in and
326 * restore these tags to the newly allocated physical page.
327 */
328void adi_restore_tags(struct mm_struct *mm, struct vm_area_struct *vma,
329 unsigned long addr, pte_t pte)
330{
331 unsigned char *tag;
332 tag_storage_desc_t *tag_desc;
333 unsigned long paddr, tmp, version1, version2;
334
335 /* Check if the swapped out page has an ADI version
336 * saved. If yes, restore version tag to the newly
337 * allocated page.
338 */
339 tag_desc = find_tag_store(mm, vma, addr);
340 if (tag_desc == NULL)
341 return;
342
343 tag = tag_start(addr, tag_desc);
344 paddr = pte_val(pte) & _PAGE_PADDR_4V;
345 for (tmp = paddr; tmp < (paddr+PAGE_SIZE); tmp += adi_blksize()) {
346 version1 = (*tag) >> 4;
347 version2 = (*tag) & 0x0f;
348 *tag++ = 0;
349 asm volatile("stxa %0, [%1] %2\n\t"
350 :
351 : "r" (version1), "r" (tmp),
352 "i" (ASI_MCD_REAL));
353 tmp += adi_blksize();
354 asm volatile("stxa %0, [%1] %2\n\t"
355 :
356 : "r" (version2), "r" (tmp),
357 "i" (ASI_MCD_REAL));
358 }
359 asm volatile("membar #Sync\n\t");
360
361 /* Check and mark this tag space for release later if
362 * the swapped in page was the last user of tag space
363 */
364 del_tag_store(tag_desc, mm);
365}
366
367/* A page is about to be swapped out. Save any ADI tags associated with
368 * this physical page so they can be restored later when the page is swapped
369 * back in.
370 */
371int adi_save_tags(struct mm_struct *mm, struct vm_area_struct *vma,
372 unsigned long addr, pte_t oldpte)
373{
374 unsigned char *tag;
375 tag_storage_desc_t *tag_desc;
376 unsigned long version1, version2, paddr, tmp;
377
378 tag_desc = alloc_tag_store(mm, vma, addr);
379 if (tag_desc == NULL)
380 return -1;
381
382 tag = tag_start(addr, tag_desc);
383 paddr = pte_val(oldpte) & _PAGE_PADDR_4V;
384 for (tmp = paddr; tmp < (paddr+PAGE_SIZE); tmp += adi_blksize()) {
385 asm volatile("ldxa [%1] %2, %0\n\t"
386 : "=r" (version1)
387 : "r" (tmp), "i" (ASI_MCD_REAL));
388 tmp += adi_blksize();
389 asm volatile("ldxa [%1] %2, %0\n\t"
390 : "=r" (version2)
391 : "r" (tmp), "i" (ASI_MCD_REAL));
392 *tag = (version1 << 4) | version2;
393 tag++;
394 }
395
396 return 0;
397}
diff --git a/arch/sparc/kernel/etrap_64.S b/arch/sparc/kernel/etrap_64.S
index 5c77a2e0e991..08cc41f64725 100644
--- a/arch/sparc/kernel/etrap_64.S
+++ b/arch/sparc/kernel/etrap_64.S
@@ -151,7 +151,32 @@ etrap_save: save %g2, -STACK_BIAS, %sp
151 stx %g6, [%sp + PTREGS_OFF + PT_V9_G6] 151 stx %g6, [%sp + PTREGS_OFF + PT_V9_G6]
152 stx %g7, [%sp + PTREGS_OFF + PT_V9_G7] 152 stx %g7, [%sp + PTREGS_OFF + PT_V9_G7]
153 or %l7, %l0, %l7 153 or %l7, %l0, %l7
154 sethi %hi(TSTATE_TSO | TSTATE_PEF), %l0 154661: sethi %hi(TSTATE_TSO | TSTATE_PEF), %l0
155 /* If userspace is using ADI, it could potentially pass
156 * a pointer with version tag embedded in it. To maintain
157 * the ADI security, we must enable PSTATE.mcde. Userspace
158 * would have already set TTE.mcd in an earlier call to
159 * kernel and set the version tag for the address being
160 * dereferenced. Setting PSTATE.mcde would ensure any
161 * access to userspace data through a system call honors
162 * ADI and does not allow a rogue app to bypass ADI by
163 * using system calls. Setting PSTATE.mcde only affects
164 * accesses to virtual addresses that have TTE.mcd set.
165 * Set PMCDPER to ensure any exceptions caused by ADI
166 * version tag mismatch are exposed before system call
167 * returns to userspace. Setting PMCDPER affects only
168 * writes to virtual addresses that have TTE.mcd set and
169 * have a version tag set as well.
170 */
171 .section .sun_m7_1insn_patch, "ax"
172 .word 661b
173 sethi %hi(TSTATE_TSO | TSTATE_PEF | TSTATE_MCDE), %l0
174 .previous
175661: nop
176 .section .sun_m7_1insn_patch, "ax"
177 .word 661b
178 .word 0xaf902001 /* wrpr %g0, 1, %pmcdper */
179 .previous
155 or %l7, %l0, %l7 180 or %l7, %l0, %l7
156 wrpr %l2, %tnpc 181 wrpr %l2, %tnpc
157 wrpr %l7, (TSTATE_PRIV | TSTATE_IE), %tstate 182 wrpr %l7, (TSTATE_PRIV | TSTATE_IE), %tstate
diff --git a/arch/sparc/kernel/process_64.c b/arch/sparc/kernel/process_64.c
index 318efd784a0b..454a8af28f13 100644
--- a/arch/sparc/kernel/process_64.c
+++ b/arch/sparc/kernel/process_64.c
@@ -670,6 +670,31 @@ int copy_thread(unsigned long clone_flags, unsigned long sp,
670 return 0; 670 return 0;
671} 671}
672 672
673/* TIF_MCDPER in thread info flags for current task is updated lazily upon
674 * a context switch. Update this flag in current task's thread flags
675 * before dup so the dup'd task will inherit the current TIF_MCDPER flag.
676 */
677int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
678{
679 if (adi_capable()) {
680 register unsigned long tmp_mcdper;
681
682 __asm__ __volatile__(
683 ".word 0x83438000\n\t" /* rd %mcdper, %g1 */
684 "mov %%g1, %0\n\t"
685 : "=r" (tmp_mcdper)
686 :
687 : "g1");
688 if (tmp_mcdper)
689 set_thread_flag(TIF_MCDPER);
690 else
691 clear_thread_flag(TIF_MCDPER);
692 }
693
694 *dst = *src;
695 return 0;
696}
697
673typedef struct { 698typedef struct {
674 union { 699 union {
675 unsigned int pr_regs[32]; 700 unsigned int pr_regs[32];
diff --git a/arch/sparc/kernel/rtrap_64.S b/arch/sparc/kernel/rtrap_64.S
index 0b21042ab181..f6528884a2c8 100644
--- a/arch/sparc/kernel/rtrap_64.S
+++ b/arch/sparc/kernel/rtrap_64.S
@@ -25,13 +25,31 @@
25 .align 32 25 .align 32
26__handle_preemption: 26__handle_preemption:
27 call SCHEDULE_USER 27 call SCHEDULE_USER
28 wrpr %g0, RTRAP_PSTATE, %pstate 28661: wrpr %g0, RTRAP_PSTATE, %pstate
29 /* If userspace is using ADI, it could potentially pass
30 * a pointer with version tag embedded in it. To maintain
31 * the ADI security, we must re-enable PSTATE.mcde before
32 * we continue execution in the kernel for another thread.
33 */
34 .section .sun_m7_1insn_patch, "ax"
35 .word 661b
36 wrpr %g0, RTRAP_PSTATE|PSTATE_MCDE, %pstate
37 .previous
29 ba,pt %xcc, __handle_preemption_continue 38 ba,pt %xcc, __handle_preemption_continue
30 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate 39 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
31 40
32__handle_user_windows: 41__handle_user_windows:
33 call fault_in_user_windows 42 call fault_in_user_windows
34 wrpr %g0, RTRAP_PSTATE, %pstate 43661: wrpr %g0, RTRAP_PSTATE, %pstate
44 /* If userspace is using ADI, it could potentially pass
45 * a pointer with version tag embedded in it. To maintain
46 * the ADI security, we must re-enable PSTATE.mcde before
47 * we continue execution in the kernel for another thread.
48 */
49 .section .sun_m7_1insn_patch, "ax"
50 .word 661b
51 wrpr %g0, RTRAP_PSTATE|PSTATE_MCDE, %pstate
52 .previous
35 ba,pt %xcc, __handle_preemption_continue 53 ba,pt %xcc, __handle_preemption_continue
36 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate 54 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
37 55
@@ -48,7 +66,16 @@ __handle_signal:
48 add %sp, PTREGS_OFF, %o0 66 add %sp, PTREGS_OFF, %o0
49 mov %l0, %o2 67 mov %l0, %o2
50 call do_notify_resume 68 call do_notify_resume
51 wrpr %g0, RTRAP_PSTATE, %pstate 69661: wrpr %g0, RTRAP_PSTATE, %pstate
70 /* If userspace is using ADI, it could potentially pass
71 * a pointer with version tag embedded in it. To maintain
72 * the ADI security, we must re-enable PSTATE.mcde before
73 * we continue execution in the kernel for another thread.
74 */
75 .section .sun_m7_1insn_patch, "ax"
76 .word 661b
77 wrpr %g0, RTRAP_PSTATE|PSTATE_MCDE, %pstate
78 .previous
52 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate 79 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
53 80
54 /* Signal delivery can modify pt_regs tstate, so we must 81 /* Signal delivery can modify pt_regs tstate, so we must
diff --git a/arch/sparc/kernel/setup_64.c b/arch/sparc/kernel/setup_64.c
index 34f7a533a74f..7944b3ca216a 100644
--- a/arch/sparc/kernel/setup_64.c
+++ b/arch/sparc/kernel/setup_64.c
@@ -294,6 +294,8 @@ static void __init sun4v_patch(void)
294 case SUN4V_CHIP_SPARC_M7: 294 case SUN4V_CHIP_SPARC_M7:
295 case SUN4V_CHIP_SPARC_M8: 295 case SUN4V_CHIP_SPARC_M8:
296 case SUN4V_CHIP_SPARC_SN: 296 case SUN4V_CHIP_SPARC_SN:
297 sun4v_patch_1insn_range(&__sun_m7_1insn_patch,
298 &__sun_m7_1insn_patch_end);
297 sun_m7_patch_2insn_range(&__sun_m7_2insn_patch, 299 sun_m7_patch_2insn_range(&__sun_m7_2insn_patch,
298 &__sun_m7_2insn_patch_end); 300 &__sun_m7_2insn_patch_end);
299 break; 301 break;
diff --git a/arch/sparc/kernel/urtt_fill.S b/arch/sparc/kernel/urtt_fill.S
index 44183aa59168..e4cee7be5cd0 100644
--- a/arch/sparc/kernel/urtt_fill.S
+++ b/arch/sparc/kernel/urtt_fill.S
@@ -50,7 +50,12 @@ user_rtt_fill_fixup_common:
50 SET_GL(0) 50 SET_GL(0)
51 .previous 51 .previous
52 52
53 wrpr %g0, RTRAP_PSTATE, %pstate 53661: wrpr %g0, RTRAP_PSTATE, %pstate
54 .section .sun_m7_1insn_patch, "ax"
55 .word 661b
56 /* Re-enable PSTATE.mcde to maintain ADI security */
57 wrpr %g0, RTRAP_PSTATE|PSTATE_MCDE, %pstate
58 .previous
54 59
55 mov %l1, %g6 60 mov %l1, %g6
56 ldx [%g6 + TI_TASK], %g4 61 ldx [%g6 + TI_TASK], %g4
diff --git a/arch/sparc/kernel/vmlinux.lds.S b/arch/sparc/kernel/vmlinux.lds.S
index 5a2344574f39..61afd787bd0c 100644
--- a/arch/sparc/kernel/vmlinux.lds.S
+++ b/arch/sparc/kernel/vmlinux.lds.S
@@ -145,6 +145,11 @@ SECTIONS
145 *(.pause_3insn_patch) 145 *(.pause_3insn_patch)
146 __pause_3insn_patch_end = .; 146 __pause_3insn_patch_end = .;
147 } 147 }
148 .sun_m7_1insn_patch : {
149 __sun_m7_1insn_patch = .;
150 *(.sun_m7_1insn_patch)
151 __sun_m7_1insn_patch_end = .;
152 }
148 .sun_m7_2insn_patch : { 153 .sun_m7_2insn_patch : {
149 __sun_m7_2insn_patch = .; 154 __sun_m7_2insn_patch = .;
150 *(.sun_m7_2insn_patch) 155 *(.sun_m7_2insn_patch)
diff --git a/arch/sparc/mm/gup.c b/arch/sparc/mm/gup.c
index 5335ba3c850e..357b6047653a 100644
--- a/arch/sparc/mm/gup.c
+++ b/arch/sparc/mm/gup.c
@@ -12,6 +12,7 @@
12#include <linux/pagemap.h> 12#include <linux/pagemap.h>
13#include <linux/rwsem.h> 13#include <linux/rwsem.h>
14#include <asm/pgtable.h> 14#include <asm/pgtable.h>
15#include <asm/adi.h>
15 16
16/* 17/*
17 * The performance critical leaf functions are made noinline otherwise gcc 18 * The performance critical leaf functions are made noinline otherwise gcc
@@ -201,6 +202,24 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
201 pgd_t *pgdp; 202 pgd_t *pgdp;
202 int nr = 0; 203 int nr = 0;
203 204
205#ifdef CONFIG_SPARC64
206 if (adi_capable()) {
207 long addr = start;
208
209 /* If userspace has passed a versioned address, kernel
210 * will not find it in the VMAs since it does not store
211 * the version tags in the list of VMAs. Storing version
212 * tags in list of VMAs is impractical since they can be
213 * changed any time from userspace without dropping into
214 * kernel. Any address search in VMAs will be done with
215 * non-versioned addresses. Ensure the ADI version bits
216 * are dropped here by sign extending the last bit before
217 * ADI bits. IOMMU does not implement version tags.
218 */
219 addr = (addr << (long)adi_nbits()) >> (long)adi_nbits();
220 start = addr;
221 }
222#endif
204 start &= PAGE_MASK; 223 start &= PAGE_MASK;
205 addr = start; 224 addr = start;
206 len = (unsigned long) nr_pages << PAGE_SHIFT; 225 len = (unsigned long) nr_pages << PAGE_SHIFT;
@@ -231,6 +250,24 @@ int get_user_pages_fast(unsigned long start, int nr_pages, int write,
231 pgd_t *pgdp; 250 pgd_t *pgdp;
232 int nr = 0; 251 int nr = 0;
233 252
253#ifdef CONFIG_SPARC64
254 if (adi_capable()) {
255 long addr = start;
256
257 /* If userspace has passed a versioned address, kernel
258 * will not find it in the VMAs since it does not store
259 * the version tags in the list of VMAs. Storing version
260 * tags in list of VMAs is impractical since they can be
261 * changed any time from userspace without dropping into
262 * kernel. Any address search in VMAs will be done with
263 * non-versioned addresses. Ensure the ADI version bits
264 * are dropped here by sign extending the last bit before
265 * ADI bits. IOMMU does not implements version tags,
266 */
267 addr = (addr << (long)adi_nbits()) >> (long)adi_nbits();
268 start = addr;
269 }
270#endif
234 start &= PAGE_MASK; 271 start &= PAGE_MASK;
235 addr = start; 272 addr = start;
236 len = (unsigned long) nr_pages << PAGE_SHIFT; 273 len = (unsigned long) nr_pages << PAGE_SHIFT;
diff --git a/arch/sparc/mm/hugetlbpage.c b/arch/sparc/mm/hugetlbpage.c
index 0112d6942288..f78793a06bbd 100644
--- a/arch/sparc/mm/hugetlbpage.c
+++ b/arch/sparc/mm/hugetlbpage.c
@@ -182,8 +182,20 @@ pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
182 struct page *page, int writeable) 182 struct page *page, int writeable)
183{ 183{
184 unsigned int shift = huge_page_shift(hstate_vma(vma)); 184 unsigned int shift = huge_page_shift(hstate_vma(vma));
185 pte_t pte;
185 186
186 return hugepage_shift_to_tte(entry, shift); 187 pte = hugepage_shift_to_tte(entry, shift);
188
189#ifdef CONFIG_SPARC64
190 /* If this vma has ADI enabled on it, turn on TTE.mcd
191 */
192 if (vma->vm_flags & VM_SPARC_ADI)
193 return pte_mkmcd(pte);
194 else
195 return pte_mknotmcd(pte);
196#else
197 return pte;
198#endif
187} 199}
188 200
189static unsigned int sun4v_huge_tte_to_shift(pte_t entry) 201static unsigned int sun4v_huge_tte_to_shift(pte_t entry)
diff --git a/arch/sparc/mm/init_64.c b/arch/sparc/mm/init_64.c
index 995f9490334d..cb9ebac6663f 100644
--- a/arch/sparc/mm/init_64.c
+++ b/arch/sparc/mm/init_64.c
@@ -3160,3 +3160,72 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end)
3160 do_flush_tlb_kernel_range(start, end); 3160 do_flush_tlb_kernel_range(start, end);
3161 } 3161 }
3162} 3162}
3163
3164void copy_user_highpage(struct page *to, struct page *from,
3165 unsigned long vaddr, struct vm_area_struct *vma)
3166{
3167 char *vfrom, *vto;
3168
3169 vfrom = kmap_atomic(from);
3170 vto = kmap_atomic(to);
3171 copy_user_page(vto, vfrom, vaddr, to);
3172 kunmap_atomic(vto);
3173 kunmap_atomic(vfrom);
3174
3175 /* If this page has ADI enabled, copy over any ADI tags
3176 * as well
3177 */
3178 if (vma->vm_flags & VM_SPARC_ADI) {
3179 unsigned long pfrom, pto, i, adi_tag;
3180
3181 pfrom = page_to_phys(from);
3182 pto = page_to_phys(to);
3183
3184 for (i = pfrom; i < (pfrom + PAGE_SIZE); i += adi_blksize()) {
3185 asm volatile("ldxa [%1] %2, %0\n\t"
3186 : "=r" (adi_tag)
3187 : "r" (i), "i" (ASI_MCD_REAL));
3188 asm volatile("stxa %0, [%1] %2\n\t"
3189 :
3190 : "r" (adi_tag), "r" (pto),
3191 "i" (ASI_MCD_REAL));
3192 pto += adi_blksize();
3193 }
3194 asm volatile("membar #Sync\n\t");
3195 }
3196}
3197EXPORT_SYMBOL(copy_user_highpage);
3198
3199void copy_highpage(struct page *to, struct page *from)
3200{
3201 char *vfrom, *vto;
3202
3203 vfrom = kmap_atomic(from);
3204 vto = kmap_atomic(to);
3205 copy_page(vto, vfrom);
3206 kunmap_atomic(vto);
3207 kunmap_atomic(vfrom);
3208
3209 /* If this platform is ADI enabled, copy any ADI tags
3210 * as well
3211 */
3212 if (adi_capable()) {
3213 unsigned long pfrom, pto, i, adi_tag;
3214
3215 pfrom = page_to_phys(from);
3216 pto = page_to_phys(to);
3217
3218 for (i = pfrom; i < (pfrom + PAGE_SIZE); i += adi_blksize()) {
3219 asm volatile("ldxa [%1] %2, %0\n\t"
3220 : "=r" (adi_tag)
3221 : "r" (i), "i" (ASI_MCD_REAL));
3222 asm volatile("stxa %0, [%1] %2\n\t"
3223 :
3224 : "r" (adi_tag), "r" (pto),
3225 "i" (ASI_MCD_REAL));
3226 pto += adi_blksize();
3227 }
3228 asm volatile("membar #Sync\n\t");
3229 }
3230}
3231EXPORT_SYMBOL(copy_highpage);
diff --git a/arch/sparc/mm/tsb.c b/arch/sparc/mm/tsb.c
index 75a04c1a2383..f5edc28aa3a5 100644
--- a/arch/sparc/mm/tsb.c
+++ b/arch/sparc/mm/tsb.c
@@ -546,6 +546,9 @@ int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
546 546
547 mm->context.sparc64_ctx_val = 0UL; 547 mm->context.sparc64_ctx_val = 0UL;
548 548
549 mm->context.tag_store = NULL;
550 spin_lock_init(&mm->context.tag_lock);
551
549#if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE) 552#if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
550 /* We reset them to zero because the fork() page copying 553 /* We reset them to zero because the fork() page copying
551 * will re-increment the counters as the parent PTEs are 554 * will re-increment the counters as the parent PTEs are
@@ -611,4 +614,22 @@ void destroy_context(struct mm_struct *mm)
611 } 614 }
612 615
613 spin_unlock_irqrestore(&ctx_alloc_lock, flags); 616 spin_unlock_irqrestore(&ctx_alloc_lock, flags);
617
618 /* If ADI tag storage was allocated for this task, free it */
619 if (mm->context.tag_store) {
620 tag_storage_desc_t *tag_desc;
621 unsigned long max_desc;
622 unsigned char *tags;
623
624 tag_desc = mm->context.tag_store;
625 max_desc = PAGE_SIZE/sizeof(tag_storage_desc_t);
626 for (i = 0; i < max_desc; i++) {
627 tags = tag_desc->tags;
628 tag_desc->tags = NULL;
629 kfree(tags);
630 tag_desc++;
631 }
632 kfree(mm->context.tag_store);
633 mm->context.tag_store = NULL;
634 }
614} 635}
diff --git a/include/linux/mm.h b/include/linux/mm.h
index ae806dbc63ee..32fe6919a11b 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -245,6 +245,9 @@ extern unsigned int kobjsize(const void *objp);
245# define VM_GROWSUP VM_ARCH_1 245# define VM_GROWSUP VM_ARCH_1
246#elif defined(CONFIG_IA64) 246#elif defined(CONFIG_IA64)
247# define VM_GROWSUP VM_ARCH_1 247# define VM_GROWSUP VM_ARCH_1
248#elif defined(CONFIG_SPARC64)
249# define VM_SPARC_ADI VM_ARCH_1 /* Uses ADI tag for access control */
250# define VM_ARCH_CLEAR VM_SPARC_ADI
248#elif !defined(CONFIG_MMU) 251#elif !defined(CONFIG_MMU)
249# define VM_MAPPED_COPY VM_ARCH_1 /* T if mapped copy of data (nommu mmap) */ 252# define VM_MAPPED_COPY VM_ARCH_1 /* T if mapped copy of data (nommu mmap) */
250#endif 253#endif
diff --git a/mm/ksm.c b/mm/ksm.c
index 293721f5da70..adb5f991da8e 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2369,6 +2369,10 @@ int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
2369 if (*vm_flags & VM_SAO) 2369 if (*vm_flags & VM_SAO)
2370 return 0; 2370 return 0;
2371#endif 2371#endif
2372#ifdef VM_SPARC_ADI
2373 if (*vm_flags & VM_SPARC_ADI)
2374 return 0;
2375#endif
2372 2376
2373 if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) { 2377 if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) {
2374 err = __ksm_enter(mm); 2378 err = __ksm_enter(mm);