Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

MIPS: DEC/SNI: O32 wrapper stack switching fixes

Commit 231a35d37293ab88d325a9cb94e5474c156282c0 [[MIPS] RM: Collected
changes] broke DECstation support by introducing an incompatible copy of
arch/mips/dec/prom/call_o32.S in arch/mips/fw/lib/, built unconditionally.
The copy happens to land earlier of the two among the modules used in the
link and is therefore chosen for the DECstation rather than the intended
original. As a result random kernel data is corrupted because a pointer
to the "%s" formatted output template is used as a temporary stack pointer
rather than being passed down to prom_printf. This also explains why
prom_printf still works, up to a point -- the next argument is the actual
string to output so it works just fine as the output template until enough
kernel data has been corrupted to cause a crash.

This change adjusts the modified wrapper in arch/mips/fw/lib/call_o32.S to
let callers request no stack switching by passing a null temporary stack
pointer in $a1, reworks the DECstation callers to work with the updated
interface and removes the old copy from arch/mips/dec/prom/call_o32.S. A
few minor readability adjustments are included as well, most importantly
O32_SZREG is now used throughout where applicable rather than hardcoded
multiplies of 4 and $fp is used to access the argument save area as a more
usual register to operate the stack with rather than $s0.

Finally an update is made to the temporary stack space used by the SNI
platform to guarantee 8-byte alignment as per o32 requirements.

Signed-off-by: Maciej W. Rozycki <macro@linux-mips.org>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: linux-mips@linux-mips.org
Patchwork: https://patchwork.linux-mips.org/patch/6668/
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>

authored by

Maciej W. Rozycki and committed by
Ralf Baechle
824122a3 af37530b

+57 -133
-1
arch/mips/dec/prom/Makefile
··· 6 6 lib-y += init.o memory.o cmdline.o identify.o console.o 7 7 8 8 lib-$(CONFIG_32BIT) += locore.o 9 - lib-$(CONFIG_64BIT) += call_o32.o
-89
arch/mips/dec/prom/call_o32.S
··· 1 - /* 2 - * O32 interface for the 64 (or N32) ABI. 3 - * 4 - * Copyright (C) 2002 Maciej W. Rozycki 5 - * 6 - * This program is free software; you can redistribute it and/or 7 - * modify it under the terms of the GNU General Public License 8 - * as published by the Free Software Foundation; either version 9 - * 2 of the License, or (at your option) any later version. 10 - */ 11 - 12 - #include <asm/asm.h> 13 - #include <asm/regdef.h> 14 - 15 - /* Maximum number of arguments supported. Must be even! */ 16 - #define O32_ARGC 32 17 - /* Number of static registers we save. */ 18 - #define O32_STATC 11 19 - /* Frame size for both of the above. */ 20 - #define O32_FRAMESZ (4 * O32_ARGC + SZREG * O32_STATC) 21 - 22 - .text 23 - 24 - /* 25 - * O32 function call dispatcher, for interfacing 32-bit ROM routines. 26 - * 27 - * The standard 64 (N32) calling sequence is supported, with a0 28 - * holding a function pointer, a1-a7 -- its first seven arguments 29 - * and the stack -- remaining ones (up to O32_ARGC, including a1-a7). 30 - * Static registers, gp and fp are preserved, v0 holds a result. 31 - * This code relies on the called o32 function for sp and ra 32 - * restoration and thus both this dispatcher and the current stack 33 - * have to be placed in a KSEGx (or KUSEG) address space. Any 34 - * pointers passed have to point to addresses within one of these 35 - * spaces as well. 36 - */ 37 - NESTED(call_o32, O32_FRAMESZ, ra) 38 - REG_SUBU sp,O32_FRAMESZ 39 - 40 - REG_S ra,O32_FRAMESZ-1*SZREG(sp) 41 - REG_S fp,O32_FRAMESZ-2*SZREG(sp) 42 - REG_S gp,O32_FRAMESZ-3*SZREG(sp) 43 - REG_S s7,O32_FRAMESZ-4*SZREG(sp) 44 - REG_S s6,O32_FRAMESZ-5*SZREG(sp) 45 - REG_S s5,O32_FRAMESZ-6*SZREG(sp) 46 - REG_S s4,O32_FRAMESZ-7*SZREG(sp) 47 - REG_S s3,O32_FRAMESZ-8*SZREG(sp) 48 - REG_S s2,O32_FRAMESZ-9*SZREG(sp) 49 - REG_S s1,O32_FRAMESZ-10*SZREG(sp) 50 - REG_S s0,O32_FRAMESZ-11*SZREG(sp) 51 - 52 - move jp,a0 53 - 54 - sll a0,a1,zero 55 - sll a1,a2,zero 56 - sll a2,a3,zero 57 - sll a3,a4,zero 58 - sw a5,0x10(sp) 59 - sw a6,0x14(sp) 60 - sw a7,0x18(sp) 61 - 62 - PTR_LA t0,O32_FRAMESZ(sp) 63 - PTR_LA t1,0x1c(sp) 64 - li t2,O32_ARGC-7 65 - 1: 66 - lw t3,(t0) 67 - REG_ADDU t0,SZREG 68 - sw t3,(t1) 69 - REG_SUBU t2,1 70 - REG_ADDU t1,4 71 - bnez t2,1b 72 - 73 - jalr jp 74 - 75 - REG_L s0,O32_FRAMESZ-11*SZREG(sp) 76 - REG_L s1,O32_FRAMESZ-10*SZREG(sp) 77 - REG_L s2,O32_FRAMESZ-9*SZREG(sp) 78 - REG_L s3,O32_FRAMESZ-8*SZREG(sp) 79 - REG_L s4,O32_FRAMESZ-7*SZREG(sp) 80 - REG_L s5,O32_FRAMESZ-6*SZREG(sp) 81 - REG_L s6,O32_FRAMESZ-5*SZREG(sp) 82 - REG_L s7,O32_FRAMESZ-4*SZREG(sp) 83 - REG_L gp,O32_FRAMESZ-3*SZREG(sp) 84 - REG_L fp,O32_FRAMESZ-2*SZREG(sp) 85 - REG_L ra,O32_FRAMESZ-1*SZREG(sp) 86 - 87 - REG_ADDU sp,O32_FRAMESZ 88 - jr ra 89 - END(call_o32)
+35 -22
arch/mips/fw/lib/call_o32.S
··· 1 1 /* 2 2 * O32 interface for the 64 (or N32) ABI. 3 3 * 4 - * Copyright (C) 2002 Maciej W. Rozycki 4 + * Copyright (C) 2002, 2014 Maciej W. Rozycki 5 5 * 6 6 * This program is free software; you can redistribute it and/or 7 7 * modify it under the terms of the GNU General Public License ··· 12 12 #include <asm/asm.h> 13 13 #include <asm/regdef.h> 14 14 15 + /* O32 register size. */ 16 + #define O32_SZREG 4 15 17 /* Maximum number of arguments supported. Must be even! */ 16 18 #define O32_ARGC 32 17 - /* Number of static registers we save. */ 19 + /* Number of static registers we save. */ 18 20 #define O32_STATC 11 19 - /* Frame size for static register */ 20 - #define O32_FRAMESZ (SZREG * O32_STATC) 21 - /* Frame size on new stack */ 22 - #define O32_FRAMESZ_NEW (SZREG + 4 * O32_ARGC) 21 + /* Argument area frame size. */ 22 + #define O32_ARGSZ (O32_SZREG * O32_ARGC) 23 + /* Static register save area frame size. */ 24 + #define O32_STATSZ (SZREG * O32_STATC) 25 + /* Stack pointer register save area frame size. */ 26 + #define O32_SPSZ SZREG 27 + /* Combined area frame size. */ 28 + #define O32_FRAMESZ (O32_ARGSZ + O32_SPSZ + O32_STATSZ) 29 + /* Switched stack frame size. */ 30 + #define O32_NFRAMESZ (O32_ARGSZ + O32_SPSZ) 23 31 24 32 .text 25 33 26 34 /* 27 35 * O32 function call dispatcher, for interfacing 32-bit ROM routines. 28 36 * 29 - * The standard 64 (N32) calling sequence is supported, with a0 30 - * holding a function pointer, a1 a new stack pointer, a2-a7 -- its 31 - * first six arguments and the stack -- remaining ones (up to O32_ARGC, 32 - * including a2-a7). Static registers, gp and fp are preserved, v0 holds 33 - * a result. This code relies on the called o32 function for sp and ra 34 - * restoration and this dispatcher has to be placed in a KSEGx (or KUSEG) 35 - * address space. Any pointers passed have to point to addresses within 36 - * one of these spaces as well. 37 + * The standard 64 (N32) calling sequence is supported, with a0 holding 38 + * a function pointer, a1 a pointer to the new stack to call the 39 + * function with or 0 if no stack switching is requested, a2-a7 -- the 40 + * function call's first six arguments, and the stack -- the remaining 41 + * arguments (up to O32_ARGC, including a2-a7). Static registers, gp 42 + * and fp are preserved, v0 holds the result. This code relies on the 43 + * called o32 function for sp and ra restoration and this dispatcher has 44 + * to be placed in a KSEGx (or KUSEG) address space. Any pointers 45 + * passed have to point to addresses within one of these spaces as well. 37 46 */ 38 47 NESTED(call_o32, O32_FRAMESZ, ra) 39 48 REG_SUBU sp,O32_FRAMESZ ··· 60 51 REG_S s0,O32_FRAMESZ-11*SZREG(sp) 61 52 62 53 move jp,a0 63 - REG_SUBU s0,a1,O32_FRAMESZ_NEW 64 - REG_S sp,O32_FRAMESZ_NEW-1*SZREG(s0) 54 + 55 + move fp,sp 56 + beqz a1,0f 57 + REG_SUBU fp,a1,O32_NFRAMESZ 58 + 0: 59 + REG_S sp,O32_NFRAMESZ-1*SZREG(fp) 65 60 66 61 sll a0,a2,zero 67 62 sll a1,a3,zero 68 63 sll a2,a4,zero 69 64 sll a3,a5,zero 70 - sw a6,0x10(s0) 71 - sw a7,0x14(s0) 65 + sw a6,4*O32_SZREG(fp) 66 + sw a7,5*O32_SZREG(fp) 72 67 73 68 PTR_LA t0,O32_FRAMESZ(sp) 74 - PTR_LA t1,0x18(s0) 69 + PTR_LA t1,6*O32_SZREG(fp) 75 70 li t2,O32_ARGC-6 76 71 1: 77 72 lw t3,(t0) 78 73 REG_ADDU t0,SZREG 79 74 sw t3,(t1) 80 75 REG_SUBU t2,1 81 - REG_ADDU t1,4 76 + REG_ADDU t1,O32_SZREG 82 77 bnez t2,1b 83 78 84 - move sp,s0 79 + move sp,fp 85 80 86 81 jalr jp 87 82 88 - REG_L sp,O32_FRAMESZ_NEW-1*SZREG(sp) 83 + REG_L sp,O32_NFRAMESZ-1*SZREG(sp) 89 84 90 85 REG_L s0,O32_FRAMESZ-11*SZREG(sp) 91 86 REG_L s1,O32_FRAMESZ-10*SZREG(sp)
+2 -1
arch/mips/fw/sni/sniprom.c
··· 40 40 41 41 #ifdef CONFIG_64BIT 42 42 43 - static u8 o32_stk[16384]; 43 + /* O32 stack has to be 8-byte aligned. */ 44 + static u64 o32_stk[4096]; 44 45 #define O32_STK &o32_stk[sizeof(o32_stk)] 45 46 46 47 #define __PROM_O32(fun, arg) fun arg __asm__(#fun); \
+20 -20
arch/mips/include/asm/dec/prom.h
··· 113 113 #define __DEC_PROM_O32(fun, arg) fun arg __asm__(#fun); \ 114 114 __asm__(#fun " = call_o32") 115 115 116 - int __DEC_PROM_O32(_rex_bootinit, (int (*)(void))); 117 - int __DEC_PROM_O32(_rex_bootread, (int (*)(void))); 118 - int __DEC_PROM_O32(_rex_getbitmap, (int (*)(memmap *), memmap *)); 116 + int __DEC_PROM_O32(_rex_bootinit, (int (*)(void), void *)); 117 + int __DEC_PROM_O32(_rex_bootread, (int (*)(void), void *)); 118 + int __DEC_PROM_O32(_rex_getbitmap, (int (*)(memmap *), void *, memmap *)); 119 119 unsigned long *__DEC_PROM_O32(_rex_slot_address, 120 - (unsigned long *(*)(int), int)); 121 - void *__DEC_PROM_O32(_rex_gettcinfo, (void *(*)(void))); 122 - int __DEC_PROM_O32(_rex_getsysid, (int (*)(void))); 123 - void __DEC_PROM_O32(_rex_clear_cache, (void (*)(void))); 120 + (unsigned long *(*)(int), void *, int)); 121 + void *__DEC_PROM_O32(_rex_gettcinfo, (void *(*)(void), void *)); 122 + int __DEC_PROM_O32(_rex_getsysid, (int (*)(void), void *)); 123 + void __DEC_PROM_O32(_rex_clear_cache, (void (*)(void), void *)); 124 124 125 - int __DEC_PROM_O32(_prom_getchar, (int (*)(void))); 126 - char *__DEC_PROM_O32(_prom_getenv, (char *(*)(char *), char *)); 127 - int __DEC_PROM_O32(_prom_printf, (int (*)(char *, ...), char *, ...)); 125 + int __DEC_PROM_O32(_prom_getchar, (int (*)(void), void *)); 126 + char *__DEC_PROM_O32(_prom_getenv, (char *(*)(char *), void *, char *)); 127 + int __DEC_PROM_O32(_prom_printf, (int (*)(char *, ...), void *, char *, ...)); 128 128 129 129 130 - #define rex_bootinit() _rex_bootinit(__rex_bootinit) 131 - #define rex_bootread() _rex_bootread(__rex_bootread) 132 - #define rex_getbitmap(x) _rex_getbitmap(__rex_getbitmap, x) 133 - #define rex_slot_address(x) _rex_slot_address(__rex_slot_address, x) 134 - #define rex_gettcinfo() _rex_gettcinfo(__rex_gettcinfo) 135 - #define rex_getsysid() _rex_getsysid(__rex_getsysid) 136 - #define rex_clear_cache() _rex_clear_cache(__rex_clear_cache) 130 + #define rex_bootinit() _rex_bootinit(__rex_bootinit, NULL) 131 + #define rex_bootread() _rex_bootread(__rex_bootread, NULL) 132 + #define rex_getbitmap(x) _rex_getbitmap(__rex_getbitmap, NULL, x) 133 + #define rex_slot_address(x) _rex_slot_address(__rex_slot_address, NULL, x) 134 + #define rex_gettcinfo() _rex_gettcinfo(__rex_gettcinfo, NULL) 135 + #define rex_getsysid() _rex_getsysid(__rex_getsysid, NULL) 136 + #define rex_clear_cache() _rex_clear_cache(__rex_clear_cache, NULL) 137 137 138 - #define prom_getchar() _prom_getchar(__prom_getchar) 139 - #define prom_getenv(x) _prom_getenv(__prom_getenv, x) 140 - #define prom_printf(x...) _prom_printf(__prom_printf, x) 138 + #define prom_getchar() _prom_getchar(__prom_getchar, NULL) 139 + #define prom_getenv(x) _prom_getenv(__prom_getenv, NULL, x) 140 + #define prom_printf(x...) _prom_printf(__prom_printf, NULL, x) 141 141 142 142 #else /* !CONFIG_64BIT */ 143 143