Skip to content

Commit 77174dd

Browse files
committed
Revert MIPS: R5900: Save and restore multimedia registers
Approach taken doesn’t work for two reasons: - SQ and LQ require 16-byte alignment, which is difficult to guarantee, for example in a general stack setting with a 4-byte natural alignment in a 32-bit kernel. Perhaps solvable with a major rewrite of MIPS exception mechanisms? The alternative approach is to save and restore multimedia registers in a separate array, in addition to the normal one. - The kernel sometimes modifies the register array. These 32-bit writes must be sign-extended to 64 bits, in a 32-bit kernel, if and only if a register was written. LQ doesn’t sign-extend. It’s not enough to compare registers with original values, because the register must be sign-extended even if it happens to have the same 32-bit value as the original: the actual act of writing the register array somewhere in the kernel must be sign-extended. The kernel writes registers both in C and in assembly (using LONG_S). Commits: - 5872154 ("MIPS: R5900: Restore multimedia registers") - eb95b1a ("MIPS: R5900: Store multimedia registers, not yet restored") - 0fa7064 ("MIPS: R5900: Introduce multimedia registers, not yet used") - 508c006 ("MIPS: R5900: Align pt_regs on stack with 16-byte boundary") - 1e44905 ("MIPS: R5900: Abstract pt_regs main array access with gprs macro") - 40e49ed ("Revert "MIPS: R5900: Reset bits 127..64 of GPRs in RESTORE_SOME"") Signed-off-by: Fredrik Noring <noring@nocrew.org>
1 parent 9b92173 commit 77174dd

22 files changed

Lines changed: 338 additions & 339 deletions

File tree

‎arch/mips/include/asm/asm.h‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,6 @@ symbol = value
249249
#define LONGLOG 3
250250
#endif
251251

252-
#ifdef CONFIG_CPU_R5900
253-
#define QUAD_L lq /* 128-bit multimedia register load */
254-
#define QUAD_S sq /* 128-bit multimedia register store */
255-
#endif
256-
257252
/*
258253
* How to add/sub/load/store/shift pointers.
259254
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static inline void __user *arch_compat_alloc_user_space(long len)
123123
struct pt_regs *regs = (struct pt_regs *)
124124
((unsigned long) current_thread_info() + THREAD_SIZE - 32) - 1;
125125

126-
return (void __user *) (regs->gprs(29) - len);
126+
return (void __user *) (regs->regs[29] - len);
127127
}
128128

129129
struct compat_ipc64_perm {

‎arch/mips/include/asm/elf.h‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -450,14 +450,14 @@ extern const char *__elf_platform;
450450
* on the MIPS.
451451
*/
452452
#define ELF_PLAT_INIT(_r, load_addr) do { \
453-
_r->gprs(1) = _r->gprs(2) = _r->gprs(3) = _r->gprs(4) = 0; \
454-
_r->gprs(5) = _r->gprs(6) = _r->gprs(7) = _r->gprs(8) = 0; \
455-
_r->gprs(9) = _r->gprs(10) = _r->gprs(11) = _r->gprs(12) = 0; \
456-
_r->gprs(13) = _r->gprs(14) = _r->gprs(15) = _r->gprs(16) = 0; \
457-
_r->gprs(17) = _r->gprs(18) = _r->gprs(19) = _r->gprs(20) = 0; \
458-
_r->gprs(21) = _r->gprs(22) = _r->gprs(23) = _r->gprs(24) = 0; \
459-
_r->gprs(25) = _r->gprs(26) = _r->gprs(27) = _r->gprs(28) = 0; \
460-
_r->gprs(30) = _r->gprs(31) = 0; \
453+
_r->regs[1] = _r->regs[2] = _r->regs[3] = _r->regs[4] = 0; \
454+
_r->regs[5] = _r->regs[6] = _r->regs[7] = _r->regs[8] = 0; \
455+
_r->regs[9] = _r->regs[10] = _r->regs[11] = _r->regs[12] = 0; \
456+
_r->regs[13] = _r->regs[14] = _r->regs[15] = _r->regs[16] = 0; \
457+
_r->regs[17] = _r->regs[18] = _r->regs[19] = _r->regs[20] = 0; \
458+
_r->regs[21] = _r->regs[22] = _r->regs[23] = _r->regs[24] = 0; \
459+
_r->regs[25] = _r->regs[26] = _r->regs[27] = _r->regs[28] = 0; \
460+
_r->regs[30] = _r->regs[31] = 0; \
461461
} while (0)
462462

463463
/* This is the location that an ET_DYN program is loaded if exec'ed. Typical

‎arch/mips/include/asm/pm.h‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@
1919
.macro SUSPEND_SAVE_REGS
2020
subu sp, PT_SIZE
2121
/* Call preserved GPRs */
22-
#ifdef CONFIG_CPU_R5900
23-
QUAD_S $16, PT_R16(sp)
24-
QUAD_S $17, PT_R17(sp)
25-
QUAD_S $18, PT_R18(sp)
26-
QUAD_S $19, PT_R19(sp)
27-
QUAD_S $20, PT_R20(sp)
28-
QUAD_S $21, PT_R21(sp)
29-
QUAD_S $22, PT_R22(sp)
30-
QUAD_S $23, PT_R23(sp)
31-
QUAD_S $28, PT_R28(sp)
32-
QUAD_S $30, PT_R30(sp)
33-
QUAD_S $31, PT_R31(sp)
34-
#else
3522
LONG_S $16, PT_R16(sp)
3623
LONG_S $17, PT_R17(sp)
3724
LONG_S $18, PT_R18(sp)
@@ -43,7 +30,6 @@
4330
LONG_S $28, PT_R28(sp)
4431
LONG_S $30, PT_R30(sp)
4532
LONG_S $31, PT_R31(sp)
46-
#endif
4733
/* A couple of CP0 registers with space in pt_regs */
4834
mfc0 k0, CP0_STATUS
4935
LONG_S k0, PT_STATUS(sp)
@@ -57,19 +43,6 @@
5743
LONG_L k0, PT_STATUS(sp)
5844
mtc0 k0, CP0_STATUS
5945
/* Call preserved GPRs */
60-
#ifdef CONFIG_CPU_R5900
61-
QUAD_L $16, PT_R16(sp)
62-
QUAD_L $17, PT_R17(sp)
63-
QUAD_L $18, PT_R18(sp)
64-
QUAD_L $19, PT_R19(sp)
65-
QUAD_L $20, PT_R20(sp)
66-
QUAD_L $21, PT_R21(sp)
67-
QUAD_L $22, PT_R22(sp)
68-
QUAD_L $23, PT_R23(sp)
69-
QUAD_L $28, PT_R28(sp)
70-
QUAD_L $30, PT_R30(sp)
71-
QUAD_L $31, PT_R31(sp)
72-
#else
7346
LONG_L $16, PT_R16(sp)
7447
LONG_L $17, PT_R17(sp)
7548
LONG_L $18, PT_R18(sp)
@@ -81,7 +54,6 @@
8154
LONG_L $28, PT_R28(sp)
8255
LONG_L $30, PT_R30(sp)
8356
LONG_L $31, PT_R31(sp)
84-
#endif
8557
/* Pop and return */
8658
jr ra
8759
addiu sp, PT_SIZE

‎arch/mips/include/asm/processor.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ unsigned long get_wchan(struct task_struct *p);
382382
THREAD_SIZE - 32 - sizeof(struct pt_regs))
383383
#define task_pt_regs(tsk) ((struct pt_regs *)__KSTK_TOS(tsk))
384384
#define KSTK_EIP(tsk) (task_pt_regs(tsk)->cp0_epc)
385-
#define KSTK_ESP(tsk) (task_pt_regs(tsk)->gprs(29))
385+
#define KSTK_ESP(tsk) (task_pt_regs(tsk)->regs[29])
386386
#define KSTK_STATUS(tsk) (task_pt_regs(tsk)->cp0_status)
387387

388388
#ifdef CONFIG_CPU_LOONGSON3

‎arch/mips/include/asm/ptrace.h‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,7 @@ struct pt_regs {
3232
#endif
3333

3434
/* Saved main processor registers. */
35-
union {
36-
unsigned long gpr; /* General purpose register */
37-
#ifdef CONFIG_CPU_R5900
38-
/* 128-bit multimedia register, 16-byte aligned for SQ/LQ */
39-
uint32_t __aligned(16) mmr[4];
40-
#endif
41-
} regs[32];
35+
unsigned long regs[32];
4236

4337
/* Saved special registers. */
4438
unsigned long cp0_status;
@@ -60,13 +54,11 @@ struct pt_regs {
6054
uint64_t lo1; /* LO1 register */
6155
#endif
6256
unsigned long __last[0];
63-
} __aligned(16);
64-
65-
#define gprs(i) regs[i].gpr
57+
} __aligned(8);
6658

6759
static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
6860
{
69-
return regs->gprs(31);
61+
return regs->regs[31];
7062
}
7163

7264
static inline void instruction_pointer_set(struct pt_regs *regs,
@@ -155,15 +147,15 @@ extern int ptrace_set_watch_regs(struct task_struct *child,
155147

156148
static inline int is_syscall_success(struct pt_regs *regs)
157149
{
158-
return !regs->gprs(7);
150+
return !regs->regs[7];
159151
}
160152

161153
static inline long regs_return_value(struct pt_regs *regs)
162154
{
163155
if (is_syscall_success(regs) || !user_mode(regs))
164-
return regs->gprs(2);
156+
return regs->regs[2];
165157
else
166-
return -regs->gprs(2);
158+
return -regs->regs[2];
167159
}
168160

169161
#define instruction_pointer(regs) ((regs)->cp0_epc)
@@ -190,13 +182,13 @@ static inline void die_if_kernel(const char *str, struct pt_regs *regs)
190182

191183
static inline unsigned long user_stack_pointer(struct pt_regs *regs)
192184
{
193-
return regs->gprs(29);
185+
return regs->regs[29];
194186
}
195187

196188
static inline void user_stack_pointer_set(struct pt_regs *regs,
197189
unsigned long val)
198190
{
199-
regs->gprs(29) = val;
191+
regs->regs[29] = val;
200192
}
201193

202194
#endif /* _ASM_PTRACE_H */

‎arch/mips/include/asm/stackframe.h‎

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@
2727
.endm
2828

2929
.macro cfi_st reg offset=0 docfi=0
30-
#ifdef CONFIG_CPU_R5900
31-
QUAD_S \reg, \offset(sp)
32-
#else
3330
LONG_S \reg, \offset(sp)
34-
#endif
3531
cfi_rel_offset \reg, \offset, \docfi
3632
.endm
3733

@@ -42,11 +38,7 @@
4238
.endm
4339

4440
.macro cfi_ld reg offset=0 docfi=0
45-
#ifdef CONFIG_CPU_R5900
46-
QUAD_L \reg, \offset(sp)
47-
#else
4841
LONG_L \reg, \offset(sp)
49-
#endif
5042
cfi_restore \reg \offset \docfi
5143
.endm
5244

@@ -264,11 +256,6 @@
264256
.set at=k1
265257
#endif
266258
PTR_SUBU sp, PT_SIZE
267-
#ifdef CONFIG_CPU_R5900
268-
/* Align struct pt_regs on 16-byte boundary for SQ/LQ MMI. */
269-
srl sp, 4
270-
sll sp, 4
271-
#endif
272259
#ifdef CONFIG_CPU_DADDI_WORKAROUNDS
273260
.set noat
274261
#endif
@@ -402,6 +389,58 @@
402389
cfi_ld sp, PT_R29, \docfi
403390
.endm
404391

392+
#ifdef CONFIG_CPU_R5900
393+
/*
394+
* Reset bits 127..64 of 128-bit multimedia registers.
395+
*
396+
* Bits 127..64 are not used by the kernel but can be modified
397+
* by applications using the R5900 specific multimedia
398+
* instructions. Clearing them prevents leaking information
399+
* between processes. This is a provisional measure until full
400+
* 128-bit registers are saved/restored, possibly using SQ/LQ.
401+
*/
402+
.macro RESET_MMR
403+
.set push
404+
.set noreorder
405+
.set noat
406+
pcpyld $1, $0, $1
407+
pcpyld $2, $0, $2
408+
pcpyld $3, $0, $3
409+
pcpyld $4, $0, $4
410+
pcpyld $5, $0, $5
411+
pcpyld $6, $0, $6
412+
pcpyld $7, $0, $7
413+
pcpyld $8, $0, $8
414+
pcpyld $9, $0, $9
415+
pcpyld $10, $0, $10
416+
pcpyld $11, $0, $11
417+
pcpyld $12, $0, $12
418+
pcpyld $13, $0, $13
419+
pcpyld $14, $0, $14
420+
pcpyld $15, $0, $15
421+
pcpyld $16, $0, $16
422+
pcpyld $17, $0, $17
423+
pcpyld $18, $0, $18
424+
pcpyld $19, $0, $19
425+
pcpyld $20, $0, $20
426+
pcpyld $21, $0, $21
427+
pcpyld $22, $0, $22
428+
pcpyld $23, $0, $23
429+
pcpyld $24, $0, $24
430+
pcpyld $25, $0, $25
431+
pcpyld $26, $0, $26
432+
pcpyld $27, $0, $27
433+
pcpyld $28, $0, $28
434+
pcpyld $29, $0, $29
435+
pcpyld $30, $0, $30
436+
pcpyld $31, $0, $31
437+
.set pop
438+
.endm
439+
#else
440+
.macro RESET_MMR
441+
.endm
442+
#endif
443+
405444
#if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
406445

407446
.macro RESTORE_SOME docfi=0
@@ -446,6 +485,7 @@
446485
.set push
447486
.set reorder
448487
.set noat
488+
RESET_MMR
449489
#ifdef CONFIG_CPU_R5900
450490
sync.p
451491
#endif

‎arch/mips/include/asm/syscall.h‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static inline bool mips_syscall_is_indirect(struct task_struct *task,
3232
/* O32 ABI syscall() - Either 64-bit with O32 or 32-bit */
3333
return (IS_ENABLED(CONFIG_32BIT) ||
3434
test_tsk_thread_flag(task, TIF_32BIT_REGS)) &&
35-
(regs->gprs(2) == __NR_syscall);
35+
(regs->regs[2] == __NR_syscall);
3636
}
3737

3838
static inline long syscall_get_nr(struct task_struct *task,
@@ -49,19 +49,19 @@ static inline void mips_syscall_update_nr(struct task_struct *task,
4949
* ends up in a0.
5050
*/
5151
if (mips_syscall_is_indirect(task, regs))
52-
task_thread_info(task)->syscall = regs->gprs(4);
52+
task_thread_info(task)->syscall = regs->regs[4];
5353
else
54-
task_thread_info(task)->syscall = regs->gprs(2);
54+
task_thread_info(task)->syscall = regs->regs[2];
5555
}
5656

5757
static inline void mips_get_syscall_arg(unsigned long *arg,
5858
struct task_struct *task, struct pt_regs *regs, unsigned int n)
5959
{
60-
unsigned long usp __maybe_unused = regs->gprs(29);
60+
unsigned long usp __maybe_unused = regs->regs[29];
6161

6262
switch (n) {
6363
case 0: case 1: case 2: case 3:
64-
*arg = regs->gprs(4 + n);
64+
*arg = regs->regs[4 + n];
6565

6666
return;
6767

@@ -78,7 +78,7 @@ static inline void mips_get_syscall_arg(unsigned long *arg,
7878
get_user(*arg, (int *)usp + n);
7979
else
8080
#endif
81-
*arg = regs->gprs(4 + n);
81+
*arg = regs->regs[4 + n];
8282

8383
return;
8484
#endif
@@ -93,13 +93,13 @@ static inline void mips_get_syscall_arg(unsigned long *arg,
9393
static inline long syscall_get_error(struct task_struct *task,
9494
struct pt_regs *regs)
9595
{
96-
return regs->gprs(7) ? -regs->gprs(2) : 0;
96+
return regs->regs[7] ? -regs->regs[2] : 0;
9797
}
9898

9999
static inline long syscall_get_return_value(struct task_struct *task,
100100
struct pt_regs *regs)
101101
{
102-
return regs->gprs(2);
102+
return regs->regs[2];
103103
}
104104

105105
static inline void syscall_rollback(struct task_struct *task,
@@ -113,11 +113,11 @@ static inline void syscall_set_return_value(struct task_struct *task,
113113
int error, long val)
114114
{
115115
if (error) {
116-
regs->gprs(2) = -error;
117-
regs->gprs(7) = 1;
116+
regs->regs[2] = -error;
117+
regs->regs[7] = 1;
118118
} else {
119-
regs->gprs(2) = val;
120-
regs->gprs(7) = 0;
119+
regs->regs[2] = val;
120+
regs->regs[7] = 0;
121121
}
122122
}
123123

0 commit comments

Comments
 (0)