Skip to contentSkip to conversation

MIPS: fix code relocation routine

RFC Applied Antony Pavlov 3 participants 5 messages Jun 18, 2019 – Jun 20, 2019 MIPS

lore
Share & tools
lore
#1 lore raw

RFC 1/2 MIPS: lib/Makefile: fix whitespaces

In reply to Antony Pavlov
---
 arch/mips/lib/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/mips/lib/Makefile +2 -2
index c600f4b533..bdcaef91a7 100644
@@ -4,8 +4,8 @@ obj-y += cpu-probe.o
 lwl-y += end.o
 obj-y += traps.o
 obj-y += genex.o
-obj-y  += reloc.o
-obj-y += sections.o 
+obj-y += reloc.o
+obj-y += sections.o
 obj-y += shutdown.o
 obj-y += dma-default.o
 
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

RFC 2/2 MIPS: relocate_code: fix barebox image memcpy() size

In reply to Antony Pavlov

In this relocate_code() piece 'length' is greater than 'barebox_image_size':

    #define MAX_BSS_SIZE SZ_1M
    ...
    length = barebox_image_size + MAX_BSS_SIZE;
    relocaddr = ALIGN_DOWN(ram_size - barebox_image_size, SZ_64K);
    ...
    memcpy((void *)relocaddr, __image_start, length);

so 'ram_size' overflow occurs during memcpy().

---
 arch/mips/lib/reloc.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
arch/mips/lib/reloc.c +5 -6
index 9756d61666..14ba6167dd 100644
@@ -40,8 +40,6 @@
 #include <linux/sizes.h>
 #include <asm-generic/memory_layout.h>
 
-#define MAX_BSS_SIZE SZ_1M
-
 void main_entry(void *fdt, u32 fdt_size);
 void relocate_code(void *fdt, u32 fdt_size, u32 relocaddr);
 
@@ -127,8 +125,10 @@ void relocate_code(void *fdt, u32 fdt_size, u32 ram_size)
 	unsigned int type;
 	long off;
 
-	length = barebox_image_size + MAX_BSS_SIZE;
-	relocaddr = ALIGN_DOWN(ram_size - barebox_image_size, SZ_64K);
+	bss_len = (unsigned long)&__bss_stop - (unsigned long)__bss_start;
+
+	length = barebox_image_size + bss_len;
+	relocaddr = ALIGN_DOWN(ram_size - length, SZ_64K);
 	relocaddr = KSEG0ADDR(relocaddr);
 	new_stack = relocaddr - MALLOC_SIZE - 16;
 
@@ -143,7 +143,7 @@ void relocate_code(void *fdt, u32 fdt_size, u32 ram_size)
 		panic("Mis-aligned relocation\n");
 
 	/* Copy Barebox to RAM */
-	memcpy((void *)relocaddr, __image_start, length);
+	memcpy((void *)relocaddr, __image_start, barebox_image_size);
 
 	/* Now apply relocations to the copy in RAM */
 	buf = __rel_start;
@@ -162,7 +162,6 @@ void relocate_code(void *fdt, u32 fdt_size, u32 ram_size)
 
 	/* Clear the .bss section */
 	bss_start = (uint8_t *)((unsigned long)__bss_start + off);
-	bss_len = (unsigned long)&__bss_stop - (unsigned long)__bss_start;
 	memset(bss_start, 0, bss_len);
 
 	 __asm__ __volatile__ (
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#0 lore raw

cover MIPS: fix code relocation routine

At the moment MIPS relocation code routine has at least two problems:

1. the first problem is fixed in the 'MIPS: relocate_code: fix barebox image size' patch from this patchseries.

2. relocate_code() calls flush_cache_all().
flush_cache_all() uses 'struct cpuinfo_mips current_cpu_data' data fields. These data fields are initialized in r4k_cache_init().

However in the current implementation the r4k_cache_init() function is called __AFTER__ relocate_code().

This cache problem can't be discovered via qemu.

Antony Pavlov (2):
MIPS: lib/Makefile: fix whitespaces
MIPS: relocate_code: fix barebox image memcpy() size

 arch/mips/lib/Makefile |  4 ++--
 arch/mips/lib/reloc.c  | 11 +++++------
 2 files changed, 7 insertions(+), 8 deletions(-)
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Tested-by on v1 2/2
#3 lore raw

Re: [RFC 2/2] MIPS: relocate_code: fix barebox image memcpy() size

In reply to Antony Pavlov

Am 18.06.19 um 11:38 schrieb Antony Pavlov:

Quoted 9 lines

In this relocate_code() piece 'length' is greater than 'barebox_image_size':

     #define MAX_BSS_SIZE SZ_1M
     ...
     length = barebox_image_size + MAX_BSS_SIZE;
     relocaddr = ALIGN_DOWN(ram_size - barebox_image_size, SZ_64K);
     ...
     memcpy((void *)relocaddr, __image_start, length);
Quoted 3 lines

so 'ram_size' overflow occurs during memcpy().

Thenk you!

This patch is fixing relocatable barebox for malta-qemu and ihas no regressions on ar9331 DPT-Module!

Quoted 41 lines
---
  arch/mips/lib/reloc.c | 11 +++++------
  1 file changed, 5 insertions(+), 6 deletions(-)
arch/mips/lib/reloc.c +5 -5
index 9756d61666..14ba6167dd 100644
@@ -40,8 +40,6 @@
  #include <linux/sizes.h>
  #include <asm-generic/memory_layout.h>
 
-#define MAX_BSS_SIZE SZ_1M
-
  void main_entry(void *fdt, u32 fdt_size);
  void relocate_code(void *fdt, u32 fdt_size, u32 relocaddr);
 
@@ -127,8 +125,10 @@ void relocate_code(void *fdt, u32 fdt_size, u32 ram_size)
  	unsigned int type;
  	long off;
 
-	length = barebox_image_size + MAX_BSS_SIZE;
-	relocaddr = ALIGN_DOWN(ram_size - barebox_image_size, SZ_64K);
+	bss_len = (unsigned long)&__bss_stop - (unsigned long)__bss_start;
+
+	length = barebox_image_size + bss_len;
+	relocaddr = ALIGN_DOWN(ram_size - length, SZ_64K);
  	relocaddr = KSEG0ADDR(relocaddr);
  	new_stack = relocaddr - MALLOC_SIZE - 16;
 
@@ -143,7 +143,7 @@ void relocate_code(void *fdt, u32 fdt_size, u32 ram_size)
  		panic("Mis-aligned relocation\n");
 
  	/* Copy Barebox to RAM */
-	memcpy((void *)relocaddr, __image_start, length);
+	memcpy((void *)relocaddr, __image_start, barebox_image_size);
 
  	/* Now apply relocations to the copy in RAM */
  	buf = __rel_start;

arch/mips/lib/reloc.c void relocate_code(void *fdt, u32 fdt_size, u32 ram_size) · line 167

Quoted 7 lines
arch/mips/lib/reloc.c +0 -1
@@ -162,7 +162,6 @@ void relocate_code(void *fdt, u32 fdt_size, u32 ram_size)
 
  	/* Clear the .bss section */
  	bss_start = (uint8_t *)((unsigned long)__bss_start + off);
-	bss_len = (unsigned long)&__bss_stop - (unsigned long)__bss_start;
  	memset(bss_start, 0, bss_len);
 
  	 __asm__ __volatile__ (

--
Regards,
Oleksij

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Applied
#4 lore raw
In reply to Antony Pavlov
Quoted 9 lines · Antony Pavlov

At the moment MIPS relocation code routine has at least two problems:

1. the first problem is fixed in the 'MIPS: relocate_code: fix barebox image size' patch from this patchseries.

2. relocate_code() calls flush_cache_all().
flush_cache_all() uses 'struct cpuinfo_mips current_cpu_data' data fields. These data fields are initialized in r4k_cache_init().

Quoted 8 lines

However in the current implementation the r4k_cache_init() function is called __AFTER__ relocate_code().

This cache problem can't be discovered via qemu.

Antony Pavlov (2):
MIPS: lib/Makefile: fix whitespaces
MIPS: relocate_code: fix barebox image memcpy() size

Applied, thanks

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox