Skip to content

Commit 517ffce

Browse files
committed
sparc64: Make montmul/montsqr/mpmul usable in 32-bit threads.
The Montgomery Multiply, Montgomery Square, and Multiple-Precision Multiply instructions work by loading a combination of the floating point and multiple register windows worth of integer registers with the inputs. These values are 64-bit. But for 32-bit userland processes we only save the low 32-bits of each integer register during a register spill. This is because the register window save area is in the user stack and has a fixed layout. Therefore, the only way to use these instruction in 32-bit mode is to perform the following sequence: 1) Load the top-32bits of a choosen integer register with a sentinel, say "-1". This will be in the outer-most register window. The idea is that we're trying to see if the outer-most register window gets spilled, and thus the 64-bit values were truncated. 2) Load all the inputs for the montmul/montsqr/mpmul instruction, down to the inner-most register window. 3) Execute the opcode. 4) Traverse back up to the outer-most register window. 5) Check the sentinel, if it's still "-1" store the results. Otherwise retry the entire sequence. This retry is extremely troublesome. If you're just unlucky and an interrupt or other trap happens, it'll push that outer-most window to the stack and clear the sentinel when we restore it. We could retry forever and never make forward progress if interrupts arrive at a fast enough rate (consider perf events as one example). So we have do limited retries and fallback to software which is extremely non-deterministic. Luckily it's very straightforward to provide a mechanism to let 32-bit applications use a 64-bit stack. Stacks in 64-bit mode are biased by 2047 bytes, which means that the lowest bit is set in the actual %sp register value. So if we see bit zero set in a 32-bit application's stack we treat it like a 64-bit stack. Runtime detection of such a facility is tricky, and cumbersome at best. For example, just trying to use a biased stack and seeing if it works is hard to recover from (the signal handler will need to use an alt stack, plus something along the lines of longjmp). Therefore, we add a system call to report a bitmask of arch specific features like this in a cheap and less hairy way. With help from Andy Polyakov. Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1d47091 commit 517ffce

13 files changed

Lines changed: 117 additions & 61 deletions

File tree

‎arch/sparc/include/asm/compat.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,10 @@ static inline void __user *arch_compat_alloc_user_space(long len)
232232
struct pt_regs *regs = current_thread_info()->kregs;
233233
unsigned long usp = regs->u_regs[UREG_I6];
234234

235-
if (!(test_thread_flag(TIF_32BIT)))
235+
if (test_thread_64bit_stack(usp))
236236
usp += STACK_BIAS;
237-
else
237+
238+
if (test_thread_flag(TIF_32BIT))
238239
usp &= 0xffffffffUL;
239240

240241
usp -= len;

‎arch/sparc/include/asm/thread_info_64.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ static inline bool test_and_clear_restore_sigmask(void)
259259

260260
#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
261261

262+
#define thread32_stack_is_64bit(__SP) (((__SP) & 0x1) != 0)
263+
#define test_thread_64bit_stack(__SP) \
264+
((test_thread_flag(TIF_32BIT) && !thread32_stack_is_64bit(__SP)) ? \
265+
false : true)
266+
262267
#endif /* !__ASSEMBLY__ */
263268

264269
#endif /* __KERNEL__ */

‎arch/sparc/include/asm/ttable.h‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,9 @@ etrap_spill_fixup_64bit: \
372372

373373
/* Normal 32bit spill */
374374
#define SPILL_2_GENERIC(ASI) \
375-
srl %sp, 0, %sp; \
375+
and %sp, 1, %g3; \
376+
brnz,pn %g3, (. - (128 + 4)); \
377+
srl %sp, 0, %sp; \
376378
stwa %l0, [%sp + %g0] ASI; \
377379
mov 0x04, %g3; \
378380
stwa %l1, [%sp + %g3] ASI; \
@@ -398,14 +400,16 @@ etrap_spill_fixup_64bit: \
398400
stwa %i6, [%g1 + %g0] ASI; \
399401
stwa %i7, [%g1 + %g3] ASI; \
400402
saved; \
401-
retry; nop; nop; \
403+
retry; \
402404
b,a,pt %xcc, spill_fixup_dax; \
403405
b,a,pt %xcc, spill_fixup_mna; \
404406
b,a,pt %xcc, spill_fixup;
405407

406408
#define SPILL_2_GENERIC_ETRAP \
407409
etrap_user_spill_32bit: \
408-
srl %sp, 0, %sp; \
410+
and %sp, 1, %g3; \
411+
brnz,pn %g3, etrap_user_spill_64bit; \
412+
srl %sp, 0, %sp; \
409413
stwa %l0, [%sp + 0x00] %asi; \
410414
stwa %l1, [%sp + 0x04] %asi; \
411415
stwa %l2, [%sp + 0x08] %asi; \
@@ -427,7 +431,7 @@ etrap_user_spill_32bit: \
427431
ba,pt %xcc, etrap_save; \
428432
wrpr %g1, %cwp; \
429433
nop; nop; nop; nop; \
430-
nop; nop; nop; nop; \
434+
nop; nop; \
431435
ba,a,pt %xcc, etrap_spill_fixup_32bit; \
432436
ba,a,pt %xcc, etrap_spill_fixup_32bit; \
433437
ba,a,pt %xcc, etrap_spill_fixup_32bit;
@@ -592,7 +596,9 @@ user_rtt_fill_64bit: \
592596

593597
/* Normal 32bit fill */
594598
#define FILL_2_GENERIC(ASI) \
595-
srl %sp, 0, %sp; \
599+
and %sp, 1, %g3; \
600+
brnz,pn %g3, (. - (128 + 4)); \
601+
srl %sp, 0, %sp; \
596602
lduwa [%sp + %g0] ASI, %l0; \
597603
mov 0x04, %g2; \
598604
mov 0x08, %g3; \
@@ -616,14 +622,16 @@ user_rtt_fill_64bit: \
616622
lduwa [%g1 + %g3] ASI, %i6; \
617623
lduwa [%g1 + %g5] ASI, %i7; \
618624
restored; \
619-
retry; nop; nop; nop; nop; \
625+
retry; nop; nop; \
620626
b,a,pt %xcc, fill_fixup_dax; \
621627
b,a,pt %xcc, fill_fixup_mna; \
622628
b,a,pt %xcc, fill_fixup;
623629

624630
#define FILL_2_GENERIC_RTRAP \
625631
user_rtt_fill_32bit: \
626-
srl %sp, 0, %sp; \
632+
and %sp, 1, %g3; \
633+
brnz,pn %g3, user_rtt_fill_64bit; \
634+
srl %sp, 0, %sp; \
627635
lduwa [%sp + 0x00] %asi, %l0; \
628636
lduwa [%sp + 0x04] %asi, %l1; \
629637
lduwa [%sp + 0x08] %asi, %l2; \
@@ -643,7 +651,7 @@ user_rtt_fill_32bit: \
643651
ba,pt %xcc, user_rtt_pre_restore; \
644652
restored; \
645653
nop; nop; nop; nop; nop; \
646-
nop; nop; nop; nop; nop; \
654+
nop; nop; nop; \
647655
ba,a,pt %xcc, user_rtt_fill_fixup; \
648656
ba,a,pt %xcc, user_rtt_fill_fixup; \
649657
ba,a,pt %xcc, user_rtt_fill_fixup;

‎arch/sparc/include/uapi/asm/unistd.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,12 @@
405405
#define __NR_setns 337
406406
#define __NR_process_vm_readv 338
407407
#define __NR_process_vm_writev 339
408+
#define __NR_kern_features 340
408409

409-
#define NR_syscalls 340
410+
#define NR_syscalls 341
411+
412+
/* Bitmask values returned from kern_features system call. */
413+
#define KERN_FEATURE_MIXED_MODE_STACK 0x00000001
410414

411415
#ifdef __32bit_syscall_numbers__
412416
/* Sparc 32-bit only has the "setresuid32", "getresuid32" variants,

‎arch/sparc/kernel/perf_event.c‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,15 +1762,25 @@ static void perf_callchain_user_32(struct perf_callchain_entry *entry,
17621762

17631763
ufp = regs->u_regs[UREG_I6] & 0xffffffffUL;
17641764
do {
1765-
struct sparc_stackf32 *usf, sf;
17661765
unsigned long pc;
17671766

1768-
usf = (struct sparc_stackf32 *) ufp;
1769-
if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
1770-
break;
1767+
if (thread32_stack_is_64bit(ufp)) {
1768+
struct sparc_stackf *usf, sf;
17711769

1772-
pc = sf.callers_pc;
1773-
ufp = (unsigned long)sf.fp;
1770+
ufp += STACK_BIAS;
1771+
usf = (struct sparc_stackf *) ufp;
1772+
if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
1773+
break;
1774+
pc = sf.callers_pc & 0xffffffff;
1775+
ufp = ((unsigned long) sf.fp) & 0xffffffff;
1776+
} else {
1777+
struct sparc_stackf32 *usf, sf;
1778+
usf = (struct sparc_stackf32 *) ufp;
1779+
if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
1780+
break;
1781+
pc = sf.callers_pc;
1782+
ufp = (unsigned long)sf.fp;
1783+
}
17741784
perf_callchain_store(entry, pc);
17751785
} while (entry->nr < PERF_MAX_STACK_DEPTH);
17761786
}

‎arch/sparc/kernel/process_64.c‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,16 @@ void flush_thread(void)
452452
/* It's a bit more tricky when 64-bit tasks are involved... */
453453
static unsigned long clone_stackframe(unsigned long csp, unsigned long psp)
454454
{
455+
bool stack_64bit = test_thread_64bit_stack(psp);
455456
unsigned long fp, distance, rval;
456457

457-
if (!(test_thread_flag(TIF_32BIT))) {
458+
if (stack_64bit) {
458459
csp += STACK_BIAS;
459460
psp += STACK_BIAS;
460461
__get_user(fp, &(((struct reg_window __user *)psp)->ins[6]));
461462
fp += STACK_BIAS;
463+
if (test_thread_flag(TIF_32BIT))
464+
fp &= 0xffffffff;
462465
} else
463466
__get_user(fp, &(((struct reg_window32 __user *)psp)->ins[6]));
464467

@@ -472,7 +475,7 @@ static unsigned long clone_stackframe(unsigned long csp, unsigned long psp)
472475
rval = (csp - distance);
473476
if (copy_in_user((void __user *) rval, (void __user *) psp, distance))
474477
rval = 0;
475-
else if (test_thread_flag(TIF_32BIT)) {
478+
else if (!stack_64bit) {
476479
if (put_user(((u32)csp),
477480
&(((struct reg_window32 __user *)rval)->ins[6])))
478481
rval = 0;
@@ -507,18 +510,18 @@ void synchronize_user_stack(void)
507510

508511
flush_user_windows();
509512
if ((window = get_thread_wsaved()) != 0) {
510-
int winsize = sizeof(struct reg_window);
511-
int bias = 0;
512-
513-
if (test_thread_flag(TIF_32BIT))
514-
winsize = sizeof(struct reg_window32);
515-
else
516-
bias = STACK_BIAS;
517-
518513
window -= 1;
519514
do {
520-
unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
521515
struct reg_window *rwin = &t->reg_window[window];
516+
int winsize = sizeof(struct reg_window);
517+
unsigned long sp;
518+
519+
sp = t->rwbuf_stkptrs[window];
520+
521+
if (test_thread_64bit_stack(sp))
522+
sp += STACK_BIAS;
523+
else
524+
winsize = sizeof(struct reg_window32);
522525

523526
if (!copy_to_user((char __user *)sp, rwin, winsize)) {
524527
shift_window_buffer(window, get_thread_wsaved() - 1, t);
@@ -544,22 +547,23 @@ void fault_in_user_windows(void)
544547
{
545548
struct thread_info *t = current_thread_info();
546549
unsigned long window;
547-
int winsize = sizeof(struct reg_window);
548-
int bias = 0;
549-
550-
if (test_thread_flag(TIF_32BIT))
551-
winsize = sizeof(struct reg_window32);
552-
else
553-
bias = STACK_BIAS;
554550

555551
flush_user_windows();
556552
window = get_thread_wsaved();
557553

558554
if (likely(window != 0)) {
559555
window -= 1;
560556
do {
561-
unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
562557
struct reg_window *rwin = &t->reg_window[window];
558+
int winsize = sizeof(struct reg_window);
559+
unsigned long sp;
560+
561+
sp = t->rwbuf_stkptrs[window];
562+
563+
if (test_thread_64bit_stack(sp))
564+
sp += STACK_BIAS;
565+
else
566+
winsize = sizeof(struct reg_window32);
563567

564568
if (unlikely(sp & 0x7UL))
565569
stack_unaligned(sp);

‎arch/sparc/kernel/ptrace_64.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static int regwindow64_get(struct task_struct *target,
151151
{
152152
unsigned long rw_addr = regs->u_regs[UREG_I6];
153153

154-
if (test_tsk_thread_flag(current, TIF_32BIT)) {
154+
if (!test_thread_64bit_stack(rw_addr)) {
155155
struct reg_window32 win32;
156156
int i;
157157

@@ -176,7 +176,7 @@ static int regwindow64_set(struct task_struct *target,
176176
{
177177
unsigned long rw_addr = regs->u_regs[UREG_I6];
178178

179-
if (test_tsk_thread_flag(current, TIF_32BIT)) {
179+
if (!test_thread_64bit_stack(rw_addr)) {
180180
struct reg_window32 win32;
181181
int i;
182182

‎arch/sparc/kernel/sys_sparc_64.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,3 +751,8 @@ int kernel_execve(const char *filename,
751751
: "cc");
752752
return __res;
753753
}
754+
755+
asmlinkage long sys_kern_features(void)
756+
{
757+
return KERN_FEATURE_MIXED_MODE_STACK;
758+
}

‎arch/sparc/kernel/systbls_64.S‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ sys_call_table32:
8686
.word compat_sys_pwritev, compat_sys_rt_tgsigqueueinfo, sys_perf_event_open, compat_sys_recvmmsg, sys_fanotify_init
8787
/*330*/ .word sys32_fanotify_mark, sys_prlimit64, sys_name_to_handle_at, compat_sys_open_by_handle_at, compat_sys_clock_adjtime
8888
.word sys_syncfs, compat_sys_sendmmsg, sys_setns, compat_sys_process_vm_readv, compat_sys_process_vm_writev
89+
/*340*/ .word sys_kern_features
8990

9091
#endif /* CONFIG_COMPAT */
9192

@@ -163,3 +164,4 @@ sys_call_table:
163164
.word sys_pwritev, sys_rt_tgsigqueueinfo, sys_perf_event_open, sys_recvmmsg, sys_fanotify_init
164165
/*330*/ .word sys_fanotify_mark, sys_prlimit64, sys_name_to_handle_at, sys_open_by_handle_at, sys_clock_adjtime
165166
.word sys_syncfs, sys_sendmmsg, sys_setns, sys_process_vm_readv, sys_process_vm_writev
167+
/*340*/ .word sys_kern_features

‎arch/sparc/kernel/unaligned_64.c‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,41 +113,49 @@ static inline long sign_extend_imm13(long imm)
113113

114114
static unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
115115
{
116-
unsigned long value;
116+
unsigned long value, fp;
117117

118118
if (reg < 16)
119119
return (!reg ? 0 : regs->u_regs[reg]);
120+
121+
fp = regs->u_regs[UREG_FP];
122+
120123
if (regs->tstate & TSTATE_PRIV) {
121124
struct reg_window *win;
122-
win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
125+
win = (struct reg_window *)(fp + STACK_BIAS);
123126
value = win->locals[reg - 16];
124-
} else if (test_thread_flag(TIF_32BIT)) {
127+
} else if (!test_thread_64bit_stack(fp)) {
125128
struct reg_window32 __user *win32;
126-
win32 = (struct reg_window32 __user *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
129+
win32 = (struct reg_window32 __user *)((unsigned long)((u32)fp));
127130
get_user(value, &win32->locals[reg - 16]);
128131
} else {
129132
struct reg_window __user *win;
130-
win = (struct reg_window __user *)(regs->u_regs[UREG_FP] + STACK_BIAS);
133+
win = (struct reg_window __user *)(fp + STACK_BIAS);
131134
get_user(value, &win->locals[reg - 16]);
132135
}
133136
return value;
134137
}
135138

136139
static unsigned long *fetch_reg_addr(unsigned int reg, struct pt_regs *regs)
137140
{
141+
unsigned long fp;
142+
138143
if (reg < 16)
139144
return &regs->u_regs[reg];
145+
146+
fp = regs->u_regs[UREG_FP];
147+
140148
if (regs->tstate & TSTATE_PRIV) {
141149
struct reg_window *win;
142-
win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
150+
win = (struct reg_window *)(fp + STACK_BIAS);
143151
return &win->locals[reg - 16];
144-
} else if (test_thread_flag(TIF_32BIT)) {
152+
} else if (!test_thread_64bit_stack(fp)) {
145153
struct reg_window32 *win32;
146-
win32 = (struct reg_window32 *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
154+
win32 = (struct reg_window32 *)((unsigned long)((u32)fp));
147155
return (unsigned long *)&win32->locals[reg - 16];
148156
} else {
149157
struct reg_window *win;
150-
win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
158+
win = (struct reg_window *)(fp + STACK_BIAS);
151159
return &win->locals[reg - 16];
152160
}
153161
}
@@ -392,13 +400,15 @@ int handle_popc(u32 insn, struct pt_regs *regs)
392400
if (rd)
393401
regs->u_regs[rd] = ret;
394402
} else {
395-
if (test_thread_flag(TIF_32BIT)) {
403+
unsigned long fp = regs->u_regs[UREG_FP];
404+
405+
if (!test_thread_64bit_stack(fp)) {
396406
struct reg_window32 __user *win32;
397-
win32 = (struct reg_window32 __user *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
407+
win32 = (struct reg_window32 __user *)((unsigned long)((u32)fp));
398408
put_user(ret, &win32->locals[rd - 16]);
399409
} else {
400410
struct reg_window __user *win;
401-
win = (struct reg_window __user *)(regs->u_regs[UREG_FP] + STACK_BIAS);
411+
win = (struct reg_window __user *)(fp + STACK_BIAS);
402412
put_user(ret, &win->locals[rd - 16]);
403413
}
404414
}
@@ -554,7 +564,7 @@ void handle_ld_nf(u32 insn, struct pt_regs *regs)
554564
reg[0] = 0;
555565
if ((insn & 0x780000) == 0x180000)
556566
reg[1] = 0;
557-
} else if (test_thread_flag(TIF_32BIT)) {
567+
} else if (!test_thread_64bit_stack(regs->u_regs[UREG_FP])) {
558568
put_user(0, (int __user *) reg);
559569
if ((insn & 0x780000) == 0x180000)
560570
put_user(0, ((int __user *) reg) + 1);

0 commit comments

Comments
 (0)